一柏副、給字符串設(shè)置下劃線效果:
例如給字符串 ABCDEFG 設(shè)置下劃線效果
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"ABCDEFG" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}];
label.attributedText = str;
二、給字符串設(shè)置點擊效果
例如要達到以下效果
屏幕快照 2019-05-12 上午10.01.54.png
這里我們要使用的是TTTAttributedLabel 一個功能豐富的富文本開源庫.
1.設(shè)置代理
@interface SHSignRegViewController ()<TTTAttributedLabelDelegate>
2.設(shè)置lable
NSString *termOfUse = Localized(@"《用戶使用協(xié)議》");
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.實現(xiàn)代理方法
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([url.absoluteString isEqualToString:@"teamRange"]) {
//響應(yīng)點擊事件
}
}