2017-10-05 67 views
3

我使用的是iOS 11和斯威夫特4.链接不是斯威夫特4

工作我试图以编程方式产生UITextView内的链接。

所有的属性都起作用(即颜色,大小,范围等) - 但不幸的是,链接不起作用。有人可以告诉我为什么吗?

这里是我的代码:

@IBOutlet weak var textView: UITextView! 

    // Setting the attributes 
    let linkAttributes = [ 
     NSAttributedStringKey.link: URL(string: "https://www.apple.com")!, 
     NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 18.0)!, 
     NSAttributedStringKey.foregroundColor: UIColor.blue 
     ] as [NSAttributedStringKey : Any] 

    let attributedString = NSMutableAttributedString(string: "Just click here to do stuff...") 

    // Set the 'click here' substring to be the link 
    attributedString.setAttributes(linkAttributes, range: NSMakeRange(5, 10)) 

    self.textView.delegate = self 
    self.textView.attributedText = attributedString 

同样,链接似乎是正确的,但点击它不工作。

下面是截图:

Screenshot of the UITextView

回答

8

您应该启用isSelectable并为您的文本视图禁用isEditable

textView.isSelectable = true 
textView.isEditable = false 

您还可以设置里面Interface Builder中这些行为在文本视图的属性检查行为部分:

Interface Builder - Attributes inspector/Behavior

+1

谢谢!我看起来太过分了。有趣的是,从Swift3切换到Swift4这个'可编辑'和'可选'属性以某种方式在故事板内部混乱了(因为在它工作正常之前)。最好以编程方式完成 - 谢谢! – iKK