引入第三方框架TYAttributedLabel https://github.com/12207480/TYAttributedLabel
文字控件的布局
TYAttributedLabel *textLbl=[[TYAttributedLabel alloc]init];
textLbl.textColor=[UIColor blackColor];
textLbl.delegate=self;
textLbl.font=MinLabelFont;
textLbl.numberOfLines=0;
textLbl.text=@"閱讀并接受";
[self addSubview:textLbl];
[textLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.selectBtn.mas_top);
make.left.equalTo(weakSelf.selectBtn.mas_right).with.mas_offset(5);
make.right.equalTo(weakSelf.mas_right).with.mas_offset(-10);
make.height.equalTo(@(40));
}];
[textLbl appendLinkWithText:@"《服務(wù)協(xié)議》" linkFont:MinLabelFont linkColor:[UIColor colorWithhexString:@"#ffb211"] underLineStyle:kCTUnderlineStyleNone linkData:@"http://baidu.com"];
[textLbl appendText:@"针肥、"];
[textLbl appendLinkWithText:@"《須知》" linkFont:MinLabelFont linkColor:[UIColor colorWithhexString:@"#ffb211"] underLineStyle:kCTUnderlineStyleNone linkData:@"http://baidu.com"];
[textLbl appendText:@"渗鬼、"];
[textLbl appendLinkWithText:@"《服務(wù)者管理規(guī)則》" linkFont:MinLabelFont linkColor:[UIColor colorWithhexString:@"#ffb211"] underLineStyle:kCTUnderlineStyleNone linkData:@"http://baidu.com"];
點擊對應(yīng)的文字會調(diào)用相關(guān)的代理的方法
#pragma mark - Delegate
//TYAttributedLabelDelegate
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)TextRun atPoint:(CGPoint)point {
if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) {
NSString *linkStr = ((TYLinkTextStorage*)TextRun).linkData;
NSLog(@"linkStr === %@",linkStr);
KnowWebViewController *knowVc=[[KnowWebViewController alloc]init];
knowVc.url=linkStr;
[self.viewController.navigationController pushViewController:knowVc animated:YES];
}
}