您可以使用稍后截取的fake URL scheme将自定义操作添加到支持链接的任何UILabel
个可用替换项中:
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"Learn more"];
[tttLabel addLinkToURL:[NSURL URLWithString:@"action://show-help"] withRange:r];
然后,在你的TTTAttributedLabelDelegate
分中:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([[url scheme] hasPrefix:@"action"]) {
if ([[url host] hasPrefix:@"show-help"]) {
/* load help screen */
} else if ([[url host] hasPrefix:@"show-settings"]) {
/* load settings screen */
}
} else {
/* deal with http links here */
}
}
TTTAttributedLabel是OHAttributedLabel的Forking .
如果你想要一种更复杂的方法,看看Nimbus Attributed Label.它支持开箱即用定制链接.