一、给字符串设置下划线效果:
例如给字符串 ABCDEFG 设置下划线效果
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"ABCDEFG" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}];
label.attributedText = str;
二、给字符串设置点击效果
例如要达到以下效果
这里我们要使用的是TTTAttributedLabel 一个功能丰富的富文本开源库.
1.设置代理
@interface SHSignRegViewController ()<TTTAttributedLabelDelegate>
2.设置lable
NSString *termOfUse = Localized(@"《用户使用协议》");
NSString *str = [NSString stringWithFormat:@"已阅读并同意%@",termOfUse];
NSRange termRange = [str rangeOfString:termOfUse];
TTTAttributedLabel *protpcplLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(WIDTH + WIDTHMAKE(35), grayLine.bottom + 10, WIDTH - WIDTHMAKE(60), 40)];
protpcplLabel.delegate = self;
protpcplLabel.numberOfLines = 2;
NSMutableAttributedString *atributeStr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor grayColor],NSFontAttributeName :[UIFont systemFontOfSize:14]}];
protpcplLabel.text = atributeStr;
[protpcplLabel addLinkToURL:[NSURL URLWithString:@"teamRange"] withRange:termRange];
[_scrollView addSubview:protpcplLabel];
3.实现代理方法
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([url.absoluteString isEqualToString:@"teamRange"]) {
//响应点击事件
}
}