一般应用在注册界面常放置《xxxx协议》提示用户使用应用即表示已遵守相关协议,也可查看协议内容等
一段文字中部分文字颜色为A,其他部分文字颜色为B,这种固然可以通过放两个UILabel控件实现,也可以使用NSMutableAttributedString实现
- 先新建一个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);
}];
- 关于NSMutableAttributedString的一些方法
为某一范围内设置多个属性- (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
为某一范围内添加多个属性- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
为某一范围添加某一个属性- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
移除某范围内某一个属性- (void)removeAttribute:(NSString *)name range:(NSRange)range;
- 例子
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:
@"我已阅读并同意《xxxx用户服务协议》"];
[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;
此时效果:
- 添加下划线
[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需求
- 自己添加下划线
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);
}];
- 添加点击事件 点击协议可跳转到查看协议界面,如果对于点击区域不严格 可直接给label加手势 点击label即跳转
//对于点击区域不严格 可直接给label加手势 点击label即跳转
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];
}