UITextField有多个链接

时间:2018-06-02 18:40:42

标签: ios swift

我有一个看起来像这样的UITextView:

enter image description here

我希望做的是“条款和协议”下划线并在按下时显示另一个控制器并将“隐私政策”加下划线并在按下时显示另一个控制器。

我该怎么做呢?我在网上找不到任何关于它的信息。

我能够像他们一样使它们可链接:

let attributedString = NSMutableAttributedString(string: "I have read and agreed to all terms and agreements and Privacy Policies")

        attributedString.addAttribute(.link, value: "https://www.hackingwithswift.com", range: NSRange(location: 30, length: 20))
        attributedString.addAttribute(.link, value: "https://www.hackingwithswift.com", range: NSRange(location: 55, length: 16))

        conditionsText.attributedText = attributedString

但是我不想让他们思考,而是希望他们做出能够呈现另一个控制器的手势......并且让我们强调它们。

1 个答案:

答案 0 :(得分:0)

您可以通过UILabel

实现这多种方式

1-扩展UITapGestureRecognizer

extension UITapGestureRecognizer {

  func didTapLabel(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.init(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.init(x:locationOfTouchInLabel.x - textContainerOffset.x, y:   locationOfTouchInLabel.y - textContainerOffset.y);
    let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

    return NSLocationInRange(indexOfCharacter, targetRange)
  }

}

2-然后检测可点击的部分

@IBAction func tapLabel(_ gesture: UITapGestureRecognizer) {
    let text = (myLabel.text)!
    let termsRange = (text as NSString).range(of: "Terms & Conditions")
    let privacyRange = (text as NSString).range(of: "Privacy Policy")

    if gesture.didTapLabel(label: myLabel, inRange: termsRange) {
        print("Tapped terms")
    } else if gesture.didTapLabel(label: myLabel, inRange: privacyRange) {
        print("Tapped privacy")
    } else {
        print("Tapped none")
    }
}

3-用法

    var myLabel:UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    myLabel = UILabel()
    myLabel.frame = self.view.frame
    myLabel.isUserInteractionEnabled = true
    self.view.addSubview(myLabel!)
    myLabel.text = "By signing Terms & Conditions and Privacy Policy"
    let tapPress = UITapGestureRecognizer(target: self, action: #selector(self.tapLabel(_:)))           
    myLabel.addGestureRecognizer(tapPress)

}