有時(shí)候需要實(shí)現(xiàn)一個(gè)文本多個(gè)點(diǎn)擊事件的東西,如此圖中的用戶(hù)協(xié)議和隱私政策都需要點(diǎn)擊跳轉(zhuǎn)對(duì)應(yīng)的頁(yè)面,可以使用YYLabel來(lái)快速實(shí)現(xiàn)

NSString *agreementText = @"點(diǎn)擊登錄即表示已同意并同意《xxx用戶(hù)協(xié)議》與《xxx隱私政策》";
NSMutableAttributedString *text ?= [[NSMutableAttributedString alloc] initWithString:agreementText];
text.lineSpacing = 5;//行間距
text.font = [UIFont systemFontOfSize:14];
text.color = [UIColor grayColor];
[text setTextHighlightRange:NSMakeRange(13, 9) color:[UIColor redColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
? ? ? ? ? ? NSLog(@"用戶(hù)協(xié)議被點(diǎn)擊了");
? }];
? ? ? ? [text setTextHighlightRange:NSMakeRange(agreementText.length-9, 9) color:[UIColor redColor]? backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
? ? ? ? ? ?NSLog(@"隱私政策被點(diǎn)擊了");
? ? ? ? }];
YYLabel *agreementLabel = [YYLabel new];
agreementLabel.numberOfLines = 0;//多行
agreementLabel.preferredMaxLayoutWidth = kScreenWidth-85;//最大寬度
agreementLabel.attributedText = text;
[self.view addSubview:agreementLabel];