美文网首页
iOS 给UITextView中的文本增加链接

iOS 给UITextView中的文本增加链接

作者: LoveBe | 来源:发表于2020-10-10 10:28 被阅读0次

点击用户协议 跳转 协议界面

-(void)createTextView {
    UITextView *contentTV = [UITextView new];
    contentTV.editable = NO;// 非编辑状态下才可以点击Url
    contentTV.textContainerInset = UIEdgeInsetsZero;
    contentTV.delegate = self;
    [contentView addSubview:contentTV];
    [contentTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(contentTV.superview);
    }];

    NSString *htmlStr = @"&#8194;&#8194;&#8194;&#8194;感谢您在使用xxx的同时给予我们的信任!<br/>&#8194;&#8194;&#8194;&#8194;xxx尊重并保护所有使用服务用户的个人隐私信息,我们就《xxx隐私权政策》及《xxx用户协议》向您说明如下:<br/>&#8194;&#8194;&#8194;&#8194;1、为了保障您的账号与使用安全,我们需要获取读取本机识别码权限;为进行日志缓存,或为您提供语音、图片、视频等下载,我们需要获取本机存储权限。您也有权拒绝或者取消授权。<br/>&#8194;&#8194;&#8194;&#8194;2、为了给您提供更畅通的信息化办公、沟通与协同等基本功能,我们会收集、使用、存储和分享必要的信息。<br/>&#8194;&#8194;&#8194;&#8194;3、我们会采取尽可能的安全措施保护您的信息安全,包括您所在企业组织的企业控制数据以及您的个人信息。<br/>&#8194;&#8194;&#8194;&#8194;4、未经您本人同意,我们绝不会从第三方处获取、共享或向其提供您的任何信息。<br/>&#8194;&#8194;&#8194;&#8194;5、您可以对您的个人信息进行查询并修改,我们也提供账户注销的方式。<br/>&#8194;&#8194;&#8194;&#8194;您使用或继续使用我们的服务,即意味着同意我们按照本《xxx隐私权政策》收集、使用、储存和分享您的相关信息。<br/>";
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    if ([attriStr.string rangeOfString:@"《xxx隐私权政策》"].location != NSNotFound) {
        //添加属性链接的URL需要注意的地方,必须使用xxxx://xxxx的格式,不然url取不到字符串
        NSArray *array = [attriStr.string componentsSeparatedByString:@"《xxx隐私权政策》"];
        [attriStr addAttribute:NSLinkAttributeName value:@"username://PrivacyAgreement" range:NSMakeRange([array[0]length], 10)];
    }

    if ([attriStr.string rangeOfString:@"《xxx用户协议》"].location != NSNotFound) {
        NSArray *array = [attriStr.string componentsSeparatedByString:@"《xxx用户协议》"];
        [attriStr addAttribute:NSLinkAttributeName value:@"username://UserAgreement" range:NSMakeRange([array[0]length], 9)];
    }
    //设置文本字体
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.0] range:NSMakeRange(0, attriStr.string.length)];

    contentTV.attributedText = attriStr;
    //增加一个NSLinkAttributeName 属性
    NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor]};
contentTV.linkTextAttributes = linkAttributes;
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"username"]) {
        NSString *username = [URL host];
        NSString *urlStr;
        NSString *titleStr;
        if ([username isEqualToString:@"PrivacyAgreement"]) {///隐私政策
            urlStr = @"http://xxxxxxxxxx:8080/PrivacyAgreement.html";
            titleStr = @"隐私政策";
        } else if ([username isEqualToString:@"UserAgreement"]) {///用户协议
            urlStr = @"http://xxxxxxxxxx:8080/UserAgreement.html";
            titleStr = @"用户协议";
        }
        WebViewController *VC = [[WebViewController alloc] init];
        VC.urlstr = urlStr;// 网址
        VC.title = titleStr;
        [self.navigationController pushViewController:VC animated:YES];
        return NO;
    }
    return YES;
}

在代理方法回调的时候,如果没有“://”的话,在[URL scheme]方法里取不到特定的值,://前面的才是最重要的标识符

相关文章

网友评论

      本文标题:iOS 给UITextView中的文本增加链接

      本文链接:https://www.haomeiwen.com/subject/ioudpktx.html