Clickable link in TTTAttributedLabel with Swift

后端 未结 2 1341
闹比i
闹比i 2021-02-04 15:01

I want to make a UILabel with some text with a click-able links in it. Not links to webpages but to actions like I do with an UIButton. So I used

2条回答
  •  温柔的废话
    2021-02-04 15:36

    String.rangeOfString returns Range, but NSString.rangeOfString returns NSRange. So the following code should work:

    let name = "tomo"
    let string = "My name is \(name)"
    label.text = string
    let nsString = string as NSString
    let range = nsString.rangeOfString(name)
    let url = NSURL(string: "action://users/\(name)")!
    label.addLinkToURL(url, withRange: range)
    

提交回复
热议问题