效果如下圖
image.png
點擊10086部分可以響應(yīng)點擊事件
實現(xiàn)方式是用的TTTAttributedLabel
TTTAttributedLabel 是一個常用的富文本開源庫谷醉,支持各種屬性文本、數(shù)據(jù)探測器惑朦,鏈接等呢簸。
設(shè)置TTTAttributedLabel
NSString *linkText = @"10086";
NSString *promptText = [NSString stringWithFormat:@"如有疑問矮台,請聯(lián)系客服%@", linkText];
NSRange linkRange = [promptText rangeOfString:linkText];
TTTAttributedLabel *textLabel = [[TTTAttributedLabel alloc] initWithFrame: CGRectZero];
textLabel.font = [UIFont yd_font11];
textLabel.textColor = [UIColor yd_grey_adadad];
textLabel.numberOfLines = 0;
textLabel.delegate = self;
textLabel.lineBreakMode = NSLineBreakByCharWrapping;
textLabel.text = promptText;
NSDictionary *attributesDic = @{(NSString *)kCTForegroundColorAttributeName : (__bridge id)[UIColor yd_blue_FF2196F3].CGColor,
(NSString *)kCTUnderlineStyleAttributeName : @(YES)
};
textLabel.linkAttributes = attributesDic;
textLabel.activeLinkAttributes = attributesDic;
[textLabel addLinkToURL:[NSURL URLWithString:@"testURL"] withRange:linkRange];
響應(yīng)點擊事件
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([url.absoluteString isEqualToString:@"testURL"]) {
//響應(yīng)點擊事件
}
}
其他方式
image.png
使用 YYText
-(YYLabel *)protocolLabel{
if (!_protocolLabel) {
NSString *originText = @"已閱讀并同意《用戶使用協(xié)議》和《個人信息授權(quán)協(xié)議》";
NSMutableAttributedString *text1 = [[NSMutableAttributedString alloc] initWithString:originText];
text1.yy_font = [UIFont systemFontOfSize:12];
text1.yy_alignment = NSTextAlignmentLeft;
text1.yy_color = RGBCOLOR(153, 153, 153);
[text1 yy_setColor:kMainColor range:[originText rangeOfString:@"《用戶使用協(xié)議》"]];
[text1 yy_setTextHighlightRange:[originText rangeOfString:@"《用戶使用協(xié)議》"]//設(shè)置點擊的位置
color:kMainColor
backgroundColor:[UIColor groupTableViewBackgroundColor]
tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
{
NSLog(@"這里是點擊事件");
JTBOpenWebUrlViewController *vc = [[JTBOpenWebUrlViewController alloc] init];
vc.url = JTB_H5_USER_REGISTER;
[self.navigationController pushViewController:vc animated:YES];
}];
[text1 yy_setColor:kMainColor range:[originText rangeOfString:@"《個人信息授權(quán)協(xié)議》"]];
[text1 yy_setTextHighlightRange:[originText rangeOfString:@"《個人信息授權(quán)協(xié)議》"]//設(shè)置點擊的位置
color:kMainColor
backgroundColor:[UIColor groupTableViewBackgroundColor]
tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
{
NSLog(@"這里是點擊事件");
JTBOpenWebUrlViewController *vc = [[JTBOpenWebUrlViewController alloc] init];
vc.url = JTB_H5_USER_GET_INFO_AGREE;
[self.navigationController pushViewController:vc animated:YES];
}];
YYLabel *titleLb = [[YYLabel alloc] init];
titleLb.userInteractionEnabled = YES;
titleLb.numberOfLines = 2;
titleLb.attributedText = text1;
titleLb.textVerticalAlignment = YYTextVerticalAlignmentCenter;
titleLb.backgroundColor = [UIColor clearColor];
_protocolLabel = titleLb;
}
return _protocolLabel;
}