如何解决如何在单个标签中点击多个单词
我是 swift 的新手,我想在每个词上获得多个可点击的词和不同的点击手势
例如:-“请正确阅读条款和条件以及隐私政策”我需要点击“条款和条件”并打印(“条款”),当点击“隐私政策”时,它应该打印(“隐私”)
我尝试了一些东西,但没有按预期正确输出
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
解决方法
创建扩展 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,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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。