使用swift在UILabel中的NSAttributedString click事件 [英] NSAttributedString click event in UILabel using swift
问题描述
假设我有一个AttributedString:已经有一个帐户?登录!". 我将此字符串放在UILabel中. 现在,当用户单击登录!"时,当前的viewController应该转到另一个viewController或在单击登录时调用某些函数.
Suppose I have an AttributedString : "Already have an account? Sign in!". I am placing this String in UILabel. Now when a user clicks on "Sign in!", current viewController should go to another viewController or some function should be called while clicking on sign in.
任何代码或建议都可以.
Any code or suggestion should be fine.
推荐答案
某些答案状态无需使用单独的手势识别器.相反,您可以将属性文本与UITextViewDelegate
的 textView:shouldInteractWithURL:inRange:interaction:
方法来实现这一目标,例如:
There's no need to use a separate gesture recognizer as some of the answers state. Instead, you can use attributed text in combination with the UITextViewDelegate
's textView:shouldInteractWithURL:inRange:interaction:
method to achieve this, ex:
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let text = NSMutableAttributedString(string: "Already have an account? ")
text.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, text.length))
let selectablePart = NSMutableAttributedString(string: "Sign in!")
selectablePart.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, selectablePart.length))
// Add an underline to indicate this portion of text is selectable (optional)
selectablePart.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSMakeRange(0,selectablePart.length))
selectablePart.addAttribute(NSAttributedString.Key.underlineColor, value: UIColor.black, range: NSMakeRange(0, selectablePart.length))
// Add an NSLinkAttributeName with a value of an url or anything else
selectablePart.addAttribute(NSAttributedString.Key.link, value: "signin", range: NSMakeRange(0,selectablePart.length))
// Combine the non-selectable string with the selectable string
text.append(selectablePart)
// Center the text (optional)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
text.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, text.length))
// To set the link text color (optional)
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)]
// Set the text view to contain the attributed text
textView.attributedText = text
// Disable editing, but enable selectable so that the link can be selected
textView.isEditable = false
textView.isSelectable = true
// Set the delegate in order to use textView(_:shouldInteractWithURL:inRange)
textView.delegate = self
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
// **Perform sign in action here**
return false
}
}
这篇关于使用swift在UILabel中的NSAttributedString click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!