富文本方式添加超链接效果

2019-08-26  本文已影响0人  不泯iOS
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectZero];
tv.delegate = self;
tv.editable = NO;
[self.view addSubview:tv];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:@"注册即代表您已阅读并同意《注册协议》" attributes:@{NSForegroundColorAttributeName:UIColor.darkGrayColor}];
//给指定的部分添加链接
[attr addAttribute:NSLinkAttributeName value:@"https://www.baidu" range:NSMakeRange(12, 6)];
tv.attributedText = attr;
 //给链接部分更改自定义颜色
tv.linkTextAttributes = @{NSForegroundColorAttributeName:Orange_color};
[tv mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(view.mas_bottom).offset(10);
                make.height.equalTo(@30);
                make.left.equalTo(self.view.mas_left).offset(16);
                make.right.equalTo(self.view.mas_right).offset(-16);
}];
上一篇 下一篇

猜你喜欢

热点阅读