How can I accurately detect if a link is clicked inside UILabels in Swift 4?

后端 未结 6 1073
暗喜
暗喜 2021-02-07 12:24

Edit

See my answer for a full working solution:

I managed to solve this myself by using a UITextView instead of a UILabel

6条回答
  •  难免孤独
    2021-02-07 12:32

    If you don't mind rewriting you code, you should use UITextView instead of UILabel.

    You can easily detect the link by setting UITextView's dataDetectorTypesand implement the delegate function to retrieve your clicked urls.

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, 
        in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool
    

    https://developer.apple.com/documentation/uikit/uitextviewdelegate/1649337-textview

提交回复
热议问题