为什么标签tapgesture不能在Swift中工作



code:此代码edit profile显示为蓝色,但点击手势不工作。如果我点击编辑配置文件那么它总是进入tapLabel方法的else部分

总是打印print("Tapped none")…我哪里错了。请指导

 fileprivate let text = "You have not completed your profile. Kindly go to edit profile and complete your profile."
class DashboardVC: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    
    self.yellowLabel.isUserInteractionEnabled = true
    underlinetermsAndPolicy()
}
    
let termsRange = (text as NSString).range(of: "edit profile")
@objc func tapLabel(gesture: UITapGestureRecognizer) {
    view.endEditing(true)
    
    if gesture.didTapAttributedTextInLabel(label: yellowLabel, inRange: termsRange) {
        print("Tapped profile")
    }
    else {
        print("Tapped none")
    }
}
func underlinetermsAndPolicy(){
    
    yellowLabel.text = text
    let clickableString = NSMutableAttributedString(string: yellowLabel.text!)
    
    clickableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: termsRange)
    
    yellowLabel.attributedText = clickableString
    yellowLabel.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(tapLabel(gesture:))))
}
}

扩展代码:

extension UITapGestureRecognizer {
func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
    // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
    let layoutManager = NSLayoutManager()
    let textContainer = NSTextContainer(size: CGSize.zero)
    let textStorage = NSTextStorage(attributedString: label.attributedText!)
    
    // Configure layoutManager and textStorage
    layoutManager.addTextContainer(textContainer)
    textStorage.addLayoutManager(layoutManager)
    
    // Configure textContainer
    textContainer.lineFragmentPadding = 0.0
    textContainer.lineBreakMode = label.lineBreakMode
    textContainer.maximumNumberOfLines = label.numberOfLines
    let labelSize = label.bounds.size
    textContainer.size = labelSize
    
    // Find the tapped character location and compare it to the specified range
    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: locationOfTouchInLabel.y - textContainerOffset.y)
    
    let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
    
    return NSLocationInRange(indexOfCharacter, targetRange)
}
}

在控制台中总是打印这个:

利用没有

替换下一行UITapGestureRecognizer扩展

let textStorage = NSTextStorage(attributedString: label.attributedText!)

后面跟着一个

let textStorage = NSTextStorage(string: label.text ?? "")

我认为你算错了locationOfTouchInTextContainer

试试这个

let locationOfTouchInLabel = self.location(in: label)
var textBoundingBox = layoutManager.usedRect(for: textContainer)
textBoundingBox.origin = CGPoint(x: label.bounds.midX - textBoundingBox.size.width / 2, y: label.bounds.midY - textBoundingBox.size.height / 2)
let touchPoint = CGPoint(x: locationOfTouchInLabel.x - textBoundingBox.origin.x, y: locationOfTouchInLabel.y - textBoundingBox.origin.y)
let index = layoutManager.characterIndex(for: touchPoint, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

相关内容

  • 没有找到相关文章

最新更新