富文本跳转点击部分文字跳转
通常用于《XX隐私政策》
-
使用UITextView实现,设置代理
@interface LoginView()<*UITextViewDelegate*> -
textview初始化
UITextView *allow_textView = [[UITextView alloc] init];allow_textView.editable = YES;allow_textView.scrollEnabled = NO;allow_textView.textContainerInset = UIEdgeInsetsZero;allow_textView.textContainer.lineFragmentPadding = 0;allow_textView.linkTextAttributes = @{NSForegroundColorAttributeName:UIColorFromHex(0x52e4b6)};allow_textView.delegate = self;[self addSubview:allow_textView]; -
设置UITextView的富文本内容
NSString *appName = [NSBundle mainBundle].infoDictionary[@"CFBundleDisplayName"];NSString *rangeStr = [NSString stringWithFormat:@"《%@隐私政策》",appName];NSString *protocolStr = [NSString stringWithFormat:@"阅读并同意%@",rangeStr];NSRange privacyRange = [protocolStr rangeOfString:rangeStr];NSMutableAttributedString *privacyMutableAttrStr = [[NSMutableAttributedString alloc] initWithString:protocolStr attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Medium" size:12.0],NSForegroundColorAttributeName:YJSetClolor(102, 102, 102, 1)}];//给需要 点击事件的部分添加链接[privacyMutableAttrStr addAttribute:NSLinkAttributeName value:@"privacy://" range:privacyRange];allow_textView.attributedText = privacyMutableAttrStr; -
UITextView代理事件
#pragma mark — UITextview Delegate
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
//这里调用方法跳到隐私政策页面
if ([URL.scheme isEqualToString:@"privacy"]) {
NSLog(@"%s - 隐私跳转",__func__);
return NO;
}
return YES;
}
/// 禁止编辑事件,此处可以防止双击文字出现复制粘贴菜单
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
return NO;
}