2017-10-05 66 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