Clickable link in TTTAttributedLabel with Swift

I want to make UILabel with some text using click-through links. Not links to web pages, but actions like I do with UIButton . So I used TTTAttributedLabel , which works great with Objective C . Now I want to do the same in Swift , so I wrote the code below:

 self.someLabel.text = NSLocalizedString("Lost? Learn more.", comment: "") let range = self.someLabel.text!.rangeOfString(NSLocalizedString("Learn more", comment:"")) self.someLabel.addLinkToURL (NSURL(string:"action://Learn more"), withRange:NSRange (range)) 

However, I cannot get the link to work in Swift . I get an error: "Missing argument for parameter 'host' in call" for the last line.

+6
source share
1 answer

String.rangeOfString returns Range , but NSString.rangeOfString returns NSRange . Therefore, 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) 
+7
source

Source: https://habr.com/ru/post/979334/


All Articles