【问题标题】:TTTAttributedLabel links not clickable after adding a UITapGestureRcognizer on the label在标签上添加 UITapGestureRcognizer 后 TTTAttributedLabel 链接不可点击
【发布时间】:2020-07-30 13:40:58
【问题描述】:

我有一个TTTAttributedLabel 来检测标签中包含的网址,现在我还想检测@menions。 为此,我在标签中添加了UITapGestureRecognizer,每次点击时我都会检查点击的单词是否以@等开头。

我的自定义逻辑工作正常,但现在TTTAttributedLabel 的 URL 检测已损坏。我尝试将委托设置为我的 UITapGestureRecognizer 并覆盖

override func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

但仍未检测到 URL 点击。知道如何在我的标签上同时拥有这两种功能吗?

编辑,我的一些代码:

UITapGestureRcognizer 是这样初始化的:

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
        // ...........
    }

【问题讨论】:

  • 你能显示来自点击功能的代码吗?
  • 我在我的问题中添加了点击功能。

标签: ios swift uilabel uitapgesturerecognizer tttattributedlabel


【解决方案1】:

您可以尝试使用其他标签来检测#hashtags、@usernames 和超链接

https://github.com/optonaut/ActiveLabel.swift

【讨论】:

  • 看起来像一个很棒的库,可以满足我的需求,谢谢 :) 如果我们决定替换旧的 TTTAttributedLabel,我会考虑它
【解决方案2】:

我想通了。 UITapGestureRecognizer 在识别后取消点击事件,因此通过将labelTap.cancelsTouchesInView = false 设置为我的UITapGestureRegognizer,触摸甚至会传递给两个回调。而且我什至不需要实现shouldRecognizeSimultaneouslyWith otherGestureRecognizer delgate 方法,因为TTTAttributedLabel 使用触摸事件而不是 UITapGestureRecognizer` 进行 URL 检测。

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多