iOS 在HTML富文本中添加超链接

 html 标签字符串如下

    protocolConfig.title = @"个人隐私保护指引";
    protocolConfig.content = @"欢迎您使用新闻!在使用前,请充分阅读并理解<a href='https://....cn/privacy_cy_policy.jsp?v=1.2' style='color:#00A5EB'>《隐私政策》</a>和<a href='https://....cn/agreement.html' style='color:#00A5EB'>《用户使用协议》</a>,点击同意按钮代表您已知悉并同意前述协议及以下约定:<br>1.我们可能会申请位置权限,用于为您推荐本地新闻、发表信息时展示位置信息等; <br>2.我们可能会申请存储空间权限,用于您保存图片、上传图片或视频等; <br>3.我们可能会申请相机权限,用于您发表信息时拍摄图片或视频等; <br>4.我们可能会申请麦克风权限,用于您拍摄视频时录制声音; <br>5.我们可能会申请系统设备权限收集设备信息,用于为您推送新闻、确保服务的稳定性等; <br>6.我们可能会申请拨打电话权限,用于您报料时拨打报料电话。 <br>上述权限均不会默认或强制开启,我们会在相关的应用场景中向您申请。";
 

 关键代码如下

<a href='https://m.thepaper.cn/privacy_cy_policy.jsp?v=1.2' style='color:#00A5EB'>《隐私政策》</a>

 生成html富文本

    }
    NSString *preString = [NSString stringWithFormat:@"<span style=\"color:#%@\">",[defaultColor hexStringFromColor]];
    NSString *suffixString = @"</span>";
    NSString *newString = [[preString stringByAppendingString:string] stringByAppendingString:suffixString];
    
    NSDictionary *dic = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[newString dataUsingEncoding:NSUnicodeStringEncoding] options:dic documentAttributes:nil error:nil];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.maximumLineHeight = lineHeight;
    paragraphStyle.minimumLineHeight = lineHeight;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.paragraphSpacing = 1;
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                paragraphStyle,NSParagraphStyleAttributeName,
                                @0.0f,NSBaselineOffsetAttributeName,
                                font,NSFontAttributeName,
                                nil];
    [attrStr addAttributes:attributes range:NSMakeRange(0, attrStr.length)];

 

效果如图

点击蓝色部分文案就跳转到相应的网页了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值