TTTAttributedLabel *detailLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(_mycheckBox.right+8, 13, 200, 20)];
NSString *detail = @"确认《协议书》及《风险揭示书》";
NSMutableAttributedString *detailStr = [[NSMutableAttributedString alloc] initWithString:detail];
[detailStr addAttribute:(NSString *)NSForegroundColorAttributeName value:CS_Color_DeepGray range:NSMakeRange(0, 2)];
[detailStr addAttribute:(NSString *)NSForegroundColorAttributeName value:UIColorFromRGB(0x1A9BFC) range:NSMakeRange(2, 5)];
[detailStr addAttribute:(NSString *)NSForegroundColorAttributeName value:CS_Color_DeepGray range:NSMakeRange(7, 1)];
[detailStr addAttribute:(NSString *)NSForegroundColorAttributeName value:UIColorFromRGB(0x1A9BFC) range:NSMakeRange(8, 7)];
[detailStr addAttribute:(NSString *)kCTFontAttributeName value:[UIFont fontWithName:@"STHeitiSC-Light" size:13.0f] range:NSMakeRange(0, detail.length)];
[detailLabel setAttributedText:detailStr];
[detailLabel setDelegate:self];
detailLabel.numberOfLines = 0;
detailLabel.lineBreakMode = NSLineBreakByCharWrapping;
detailLabel.linkAttributes = @{
(NSString *)kCTUnderlineStyleAttributeName : @NO,
(NSString *) NSForegroundColorAttributeName : CS_Color_StandGreen2
};
NSURL *linkOne = [NSURL URLWithString:@"protocol"];//添加链接
NSURL *linkTow = [NSURL URLWithString:@"risk"];
[detailLabel addLinkToURL:linkOne withRange:NSMakeRange(2, 5)];
[detailLabel addLinkToURL:linkTow withRange:NSMakeRange(8, 7)];
//链接的实现:
#pragma mark -
#pragma mark TTTAttributedLabel Delegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
if ([url.absoluteString isEqualToString:@"protocol"])
{
[self queryProductProtocol];
}
else
{
[self queryProductRisk];
}
}
添加链接并调用的协议:
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url;
转载自--------https://my.oschina.net/u/2534563/blog/663988