对于不可编辑的 UITextView,我想在 iOS9+ 中嵌入这样的文本:
Just click here to register
我可以创建一个函数并操作文本,但有更简单的方法吗?
我发现我可以使用 NSTextCheckingTypeLink,因此在 Interface Builder 中无需“单击此处”部分即可直接单击文本:
Just http://example.com to register
如果相关的话,我正在使用 Xcode 8 和 Swift 3。
最佳答案
设置 isEditable = false
否则 TextView 将在用户点击时进入文本编辑模式。
Swift 4 及更高版本
let attributedString = NSMutableAttributedString(string: "Just click here to register")
let url = URL(string: "https://www.apple.com")!
// Set the 'click here' substring to be the link
attributedString.setAttributes([.link: url], range: NSMakeRange(5, 10))
self.textView.attributedText = attributedString
self.textView.isUserInteractionEnabled = true
self.textView.isEditable = false
// Set how links should appear: blue and underlined
self.textView.linkTextAttributes = [
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.rawValue
]
关于swift - 带有超链接文本的 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39238366/