Ios t在标签上添加UITapGestitureCognizer后,无法单击标签链接
我有一个Ios t在标签上添加UITapGestitureCognizer后,无法单击标签链接,ios,swift,uilabel,uitapgesturerecognizer,tttattributedlabel,Ios,Swift,Uilabel,Uitapgesturerecognizer,Tttattributedlabel,我有一个TTTAttributedLabel来检测标签中包含的URL,现在我还想检测@menions。 为此,我在标签上添加了一个uitappesturerecognizer,并在每次点击时检查点击的单词是否以@开头,等等 我的自定义逻辑工作正常,但现在tttatAttributedLabel的URL检测被破坏。我已尝试将代理设置为UITapGestureRecognitor并覆盖 override func gestureRecognizer(_ gestureRecognizer: UIGe
TTTAttributedLabel
来检测标签中包含的URL,现在我还想检测@menions
。
为此,我在标签上添加了一个uitappesturerecognizer
,并在每次点击时检查点击的单词是否以@开头,等等
我的自定义逻辑工作正常,但现在tttatAttributedLabel
的URL检测被破坏。我已尝试将代理设置为UITapGestureRecognitor并覆盖
override func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
但是仍然没有检测到URL点击。你知道如何在标签上同时使用这两种功能吗
编辑,我的一些代码:
UIAPTgestureCognizer初始化为:
commentLabel.isUserInteractionEnabled = true;
let labelTap = UITapGestureRecognizer(target: self, action: #selector(labelTapped(_:)))
labelTap.delegate = self
commentLabel.addGestureRecognizer(labelTap)
我的点击功能如下:
@objc func labelTapped(_ sender: UITapGestureRecognizer) {
// Find the character that was tapped...
let textLabel = sender.view as? UILabel
let tapLocation = sender.location(in: textLabel)
// init text storage
var textStorage: NSTextStorage? = nil
if let attributedText = textLabel?.attributedText {
textStorage = NSTextStorage(attributedString: attributedText)
}
let layoutManager = NSLayoutManager()
textStorage?.addLayoutManager(layoutManager)
// init text container
let textContainer = NSTextContainer(size: CGSize(width: textLabel?.frame.size.width ?? 0.0, height: (textLabel?.frame.size.height ?? 0.0) + 100))
textContainer.lineFragmentPadding = 0
textContainer.maximumNumberOfLines = textLabel?.numberOfLines ?? 0
if let lineBreakMode = textLabel?.lineBreakMode {
textContainer.lineBreakMode = lineBreakMode
}
layoutManager.addTextContainer(textContainer)
// Get the character that was tapped
let characterIndex = layoutManager.characterIndex(
for: tapLocation,
in: textContainer,
fractionOfDistanceBetweenInsertionPoints: nil)
// Now find the word that was tapped
let word = textLabel?.text?.getWordFromCharacteratIndex(characterIndex) // Another custom function I've built that returns the word form a character
// And match it with a mentioned user
// Some code to match the word with my mentions pattern
// ...........
}
您可以尝试另一个标签,在那里可以检测#hashtags、@usernames和hyperlinkes
您可以尝试使用另一个标签,在那里可以检测#hashtags、@usernames和hyperlinkes
我想出来了。UIAPTgestureRecognizer在识别tap事件后取消了该事件,因此通过将
labelTap.cancelsTouchesInView=false
设置为myUIAPTgestureRegonizer
,触摸甚至可以传递到两个回调。我甚至不需要使用其他GestureRecognizer同时实现shouldRecognizer
delgate方法,因为TTtatAttributedLabel
使用触摸事件而不是UITapGestureRecognizer`来检测URL。我明白了。UIAPTgestureRecognizer在识别tap事件后取消了该事件,因此通过将labelTap.cancelsTouchesInView=false
设置为myUIAPTgestureRegonizer
,触摸甚至可以传递到两个回调。我甚至不需要与其他手势识别器同时实现should recognize
delgate方法,由于TTTAttributedLabel
正在使用触摸事件而不是UITapGestureRecognitizer`进行URL检测。能否显示点击功能中的代码?我在问题中添加了点击功能。能否显示点击功能中的代码?我在问题中添加了点击功能。看起来像是一个很棒的库,这正是我需要的,谢谢你:“我会考虑在未来,如果我们决定取代旧的代码> TTTITAITICATION标签< /代码>看起来像一个伟大的图书馆,这是我所需要的,谢谢你:)我会考虑它在未来,如果我们决定取代旧的代码> TTTAttestDelabelaby<代码>