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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容