The idea is to use library such as TTTAttributedLabel and using it this way :
Consider the following string "I want to be touchable here" where here will perform a segue on touch.
- Create
TTTAttributedLabel
(UILabel
subclass), and put text content in it, as if it was a UILabel
. Set its delegate to self. Then, add a link to a word this way :
Objective C
NSRange rangeWord = [attributedLabel.text rangeOfString:@"here"];
[attributedLabel addLinkToURL:[NSURL URLWithString:@"anActionOnClickHere"] withRange:rangeUser];
Swift
NSRange rangeWord = attributedLabel.text.rangeOfString("here")
attributedLabel.addLinkToURL(NSURL(string: "anActionOnClickHere"), withRange:rangeUser)
- On clicking words it will call this method in which you can handle the click :
Objective C
- (void)attributedLabel:(__unused TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url {
NSString *urlToString = [url absoluteString];
if ([urlToString containsString:@"anActionOnClickHere"]) { //perform segue for example
[self performSegueWithIdentifier:@"hereSegue" sender:self];
}
}
Swift
func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
NSString *urlToString = url.absoluteString()
if (urlToString.containsString("anActionOnClickHere")) { //perform segue for example
self.performSegueWithIdentifier("hereSegue",sender:self)
}
}
With the default link style, you'll get the blue color that you're looking for.