效果如下图
image.png
点击10086部分可以响应点击事件
实现方式是用的TTTAttributedLabel
TTTAttributedLabel 是一个常用的富文本开源库,支持各种属性文本、数据探测器,链接等。
设置TTTAttributedLabel
NSString *linkText = @"10086";
NSString *promptText = [NSString stringWithFormat:@"如有疑问,请联系客服%@", 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];
响应点击事件
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([url.absoluteString isEqualToString:@"testURL"]) {
//响应点击事件
}
}
其他方式
image.png使用 YYText
-(YYLabel *)protocolLabel{
if (!_protocolLabel) {
NSString *originText = @"已阅读并同意《用户使用协议》和《个人信息授权协议》";
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:@"《用户使用协议》"]];
[text1 yy_setTextHighlightRange:[originText rangeOfString:@"《用户使用协议》"]//设置点击的位置
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:@"《个人信息授权协议》"]];
[text1 yy_setTextHighlightRange:[originText rangeOfString:@"《个人信息授权协议》"]//设置点击的位置
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;
}
网友评论