YYKit是个好东西-YYLabel实现一个文本多个点击事件
谯嘉懿
2023-12-01
NSString *agreementText = @"点击登录即表示已同意并同意《xxx用户协议》与《xxx隐私政策》";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:agreementText];
text.lineSpacing = 5;
text.font = [UIFont systemFontOfSize:14];
text.color = JDD_RGBColor_GrayContent;
[text setTextHighlightRange:NSMakeRange(13, 9) color:[UIColor redColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"用户协议被点击了");
}];
[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(@"用户协议被点击了");
}];
YYLabel *agreementLabel = [YYLabel new];
agreementLabel.numberOfLines = 0;
agreementLabel.preferredMaxLayoutWidth = kScreenWidth-85;//最大宽度
agreementLabel.attributedText = text;
[self.view addSubview:agreementLabel];