您的位置:首页 > 其它

TTTAttributedLabel第三方使用(一段连续的字符串不同范围用不同字体和颜色)

2016-04-21 00:00 218 查看
事例代码

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: