一般應(yīng)用在注冊界面常放置《xxxx協(xié)議》提示用戶使用應(yīng)用即表示已遵守相關(guān)協(xié)議,也可查看協(xié)議內(nèi)容等
一段文字中部分文字顏色為A,其他部分文字顏色為B,這種固然可以通過放兩個UILabel控件實現(xiàn),也可以使用NSMutableAttributedString實現(xiàn)
- 先新建一個UILabel
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:16];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(@0);
}];
- 關(guān)于NSMutableAttributedString的一些方法
為某一范圍內(nèi)設(shè)置多個屬性- (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
為某一范圍內(nèi)添加多個屬性- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
為某一范圍添加某一個屬性- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
移除某范圍內(nèi)某一個屬性- (void)removeAttribute:(NSString *)name range:(NSRange)range;
- 例子
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:
@"我已閱讀并同意《xxxx用戶服務(wù)協(xié)議》"];
[attributedString addAttribute:NSForegroundColorAttributeName value:
[UIColor lightGrayColor] range:NSMakeRange(0,7)];
//下面兩句代碼等效
// [attributedString addAttribute:NSForegroundColorAttributeName value:
// [UIColor orangeColor] range:NSMakeRange(7,12)];
[attributedString setAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]} range:NSMakeRange(7,12)];
label.attributedText = attributedString;
此時效果:

Simulator Screen Shot 2017年7月26日 17.50.42.png
- 添加下劃線
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(7, 12)];//下劃線類型
[attributedString addAttribute:NSUnderlineColorAttributeName value:
[UIColor blueColor] range:NSMakeRange(7, 12)];//下劃線顏色
//下劃線類型枚舉
typedef NS_ENUM(NSInteger, NSUnderlineStyle) {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02,
NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09,
NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0000,
NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100,
NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200,
NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300,
NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400,
NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000
} NS_ENUM_AVAILABLE(10_0, 6_0);
- 此時效果 -這種方式添加的下劃線 距離文字太近 一般不會符合UI需求

Simulator Screen Shot 2017年7月26日 17.52.38.png
- 自己添加下劃線
UIView *underLineView = [[UIView alloc] init];
underLineView.backgroundColor = [UIColor blueColor];
[label addSubview:underLineView];
[underLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(@1);
make.size.mas_equalTo(CGSizeMake(130, 1));
make.trailing.mas_equalTo(-17);
}];
- 添加點擊事件 點擊協(xié)議可跳轉(zhuǎn)到查看協(xié)議界面,如果對于點擊區(qū)域不嚴(yán)格 可直接給label加手勢 點擊label即跳轉(zhuǎn)
//對于點擊區(qū)域不嚴(yán)格 可直接給label加手勢 點擊label即跳轉(zhuǎn)
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture)];
[label addGestureRecognizer:tap];
- (void)tapGesture
{
SecondViewController *vc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}

Simulator Screen Shot 2017年7月26日 17.55.31.png