有了一个不可编辑的UITextView,我想嵌入这样的文字iOS9 +:的UITextView与超链接文本
只是click here注册
我可以创造一个功能和操作文本但有没有更简单的方法?
我看到,我可以用NSTextCheckingTypeLink因此让文字也可以点击没有“点击这里”部分是直接在Interface Builder:
我使用的Xcode 8和Swift 3,如果这是相关的。
有了一个不可编辑的UITextView,我想嵌入这样的文字iOS9 +:的UITextView与超链接文本
只是click here注册
我可以创造一个功能和操作文本但有没有更简单的方法?
我看到,我可以用NSTextCheckingTypeLink因此让文字也可以点击没有“点击这里”部分是直接在Interface Builder:
我使用的Xcode 8和Swift 3,如果这是相关的。
设置你的视图控制器符合UITextViewDelegate
和下面的代码:
override func viewDidLoad() {
super.viewDidLoad()
// You must set the formatting of the link manually
let linkAttributes = [
NSLinkAttributeName: NSURL(string: "https://www.apple.com")!,
NSForegroundColorAttributeName: UIColor.blue
] as [String : Any]
let attributedString = NSMutableAttributedString(string: "Just click here to register")
// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10))
self.textView.delegate = self
self.textView.attributedText = attributedString
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return true
}
我想做同样的事情,最终只用一个UIButton与UILabels包围的标题是“点击这里”“只是“和”注册“,然后:
@IBAction func btnJustClickHereLink(_ sender: UIButton) {
if let url = URL(string: "http://example.com") {
UIApplication.shared.openURL(url)
}
}
UITextItemInteraction是iOS10 +,但我需要iOS9 +。我会看看我是否可以修复并在一秒之内为此投票。 –
在swift 4中,属性已经从NSLinkAttributedName重命名为.link,NSForegroundColorAttributeName重命名为.foregroundColor –