我有一个看起来像这样的UITextView:
我要做的是在“条款和协议”下划线,并在按下时显示另一个控制器,并在按下时显示“隐私政策”,并显示另一个控制器。
我该怎么做呢?我在网上找不到任何关于它的东西。
我可以让它们像这样链接起来:
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
但我不想让他们思考,而是希望他们是一种手势,可以呈现另一种controller....and,让他们下划线。
发布于 2018-06-03 02:54:44
您可以通过许多方法来实现这一点,这里使用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)
}
https://stackoverflow.com/questions/50660041
复制相似问题