【问题标题】:How to get multiple word click in a single label如何在单个标签中获得多个单词点击
【发布时间】:2021-05-05 14:36:33
【问题描述】:

我是 swift 新手,我想在每个单词上获得多个可点击的单词和不同的点击手势

EX:-“请正确阅读条款和条件和隐私政策”我需要点击“条款和条件”并打印(“条款”),当点击“隐私政策”时,它应该打印(“隐私”)

我尝试了一些东西,但没有得到预期的正确输出

let txt = NSMutableAttributedString(string: labelCreateAccount.text!)
        let range = (labelCreateAccount.text! as NSString).range(of: "Term & Condition")
        let range1 = (labelCreateAccount.text! as NSString).range(of: "Privacy Policy")
        
        txt.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.red] , range: range)
        txt.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.red] , range: range1)
        labelCreateAccount.addGestureRecognizer(UITapGestureRecognizer(target:range, action: #selector(LabelTapAccount)))

        labelCreateAccount.addGestureRecognizer(UITapGestureRecognizer(target:range 1, action: #selector(LabelTapAccount)))

        labelCreateAccount.attributedText = txt
        labelCreateAccount.isUserInteractionEnabled = true

【问题讨论】:

  • 我认为最好的方法是添加两个不同的标签,并在其中添加交互。

标签: ios swift swift3 delegates


【解决方案1】:

创建扩展UITapGestureRecognizer

extension UITapGestureRecognizer {

    func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
        
        let layoutManager = NSLayoutManager()
        let textContainer = NSTextContainer(size: CGSize.zero)
        let textStorage = NSTextStorage(attributedString: label.attributedText!)
        
        layoutManager.addTextContainer(textContainer)
        textStorage.addLayoutManager(layoutManager)
        
        textContainer.lineFragmentPadding = 0.0
        textContainer.lineBreakMode = label.lineBreakMode
        textContainer.maximumNumberOfLines = label.numberOfLines
        let labelSize = label.bounds.size
        textContainer.size = labelSize
        
        let locationOfTouchInLabel = self.location(in: label)
        let textBoundingBox = layoutManager.usedRect(for: textContainer)
        let textContainerOffset = CGPoint(x: (labelSize.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
                                          y: (labelSize.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y);
        let locationOfTouchInTextContainer = CGPoint(x: (locationOfTouchInLabel.x - textContainerOffset.x),
                                                     y: 0 );
        
        let lineModifier = Int(ceil(locationOfTouchInLabel.y / label.font.lineHeight)) - 1
        let rightMostFirstLinePoint = CGPoint(x: labelSize.width, y: 0)
        let charsPerLine = layoutManager.characterIndex(for: rightMostFirstLinePoint, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
        
        let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
        let adjustedRange = indexOfCharacter + (lineModifier * charsPerLine)
        
        return NSLocationInRange(adjustedRange, targetRange)
    }
    
}

extension Range where Bound == String.Index {
    var nsRange: NSRange {
        return NSRange(location: self.lowerBound.encodedOffset,
                       length: self.upperBound.encodedOffset -
                        self.lowerBound.encodedOffset)
    }
}

将 TapGesture 添加到目标标签和侦听器上,检查从属性文本中点击的目标字符串:

@objc private func settingTapped(_ sender: UITapGestureRecognizer) {
    guard let range = self.yourLabel.text?.range(of: "your Text")?.nsRange else {
        return
    }
    
    if sender.didTapAttributedTextInLabel(label: self.yourLabel, inRange: range) {
        // your work
    }
    
}

现在设置点击标签:

let txt = NSMutableAttributedString(string: labelCreateAccount.text!)
            let range = (labelCreateAccount.text! as NSString).range(of: "Term & Condition")
            let range1 = (labelCreateAccount.text! as NSString).range(of: "Privacy Policy")
            
            txt.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.red] , range: range)
            txt.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.red] , range: range1)

            labelCreateAccount.attributedText = txt
            
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.settingTapped(_:)))
            self.labelCreateAccount.addGestureRecognizer(tap)
            self.labelCreateAccount.isUserInteractionEnabled = true

【讨论】:

  • 我已经看到了但无法理解
  • 你能帮我理解这个吗..?
  • 是的,首先你创建 attributeText 以在你的标签上显示然后你在标签上添加 TapGesture 现在你发现点击手势的位置包括你的文本以调用任何操作
  • 我最大的困惑是我把这个设置放在哪里录音功能主义者
  • 你实现扩展 Range where Bound == String.Index { var nsRange: NSRange { return NSRange(location: self.lowerBound.encodedOffset, length: self.upperBound.encodedOffset - self.lowerBound.encodedOffset) } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多