swift 在UILabel中创建多个可指向链接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 在UILabel中创建多个可指向链接相关的知识,希望对你有一定的参考价值。
// MARK: Multiple Tappable Links in a UILabel
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)
}
}
// add uilabel then setup
func setupMultipleTapLabel() {
lblPrivacyTerm.text = "By signing up you agree to our Terms of service and Privacy policy"
let text = (lblPrivacyTerm.text)!
let underlineAttriString = NSMutableAttributedString(string: text)
let range1 = (text as NSString).range(of: "Terms of service")
underlineAttriString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: range1)
let range2 = (text as NSString).range(of: "Privacy policy")
underlineAttriString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: range2)
lblPrivacyTerm.attributedText = underlineAttriString
let tapAction = UITapGestureRecognizer(target: self, action: #selector(self.tapLabel(gesture:)))
lblPrivacyTerm.isUserInteractionEnabled = true
lblPrivacyTerm.addGestureRecognizer(tapAction)
}
@IBAction func tapLabel(gesture: UITapGestureRecognizer) {
let text = (lblPrivacyTerm.text)!
let termsRange = (text as NSString).range(of: "Terms of service")
let privacyRange = (text as NSString).range(of: "Privacy policy")
if gesture.didTapAttributedTextInLabel(label: lblPrivacyTerm, inRange: termsRange) {
print("Terms of service")
} else if gesture.didTapAttributedTextInLabel(label: lblPrivacyTerm, inRange: privacyRange) {
print("Privacy policy")
} else {
print("Tapped none")
}
}
以上是关于swift 在UILabel中创建多个可指向链接的主要内容,如果未能解决你的问题,请参考以下文章
当他们在 Swift 中创建自己时如何更改 UILabel 的标题?
在 CloudKit 中创建指向多个 CKRecord 的引用列表
如何在 Swift IOS 中创建具有多个堆栈视图的可滚动堆栈视图
如何在 Pandas 和 Jupyter Notebook 中创建带有指向本地文件的可单击超链接的表