UITextView富文本实现跳转链接并实现文本居中-- 服务协议、隐私条款

实现效果


注册登录

UITextView *protocolTV = [[UITextView alloc] initWithFrame:CGRectZero];

protocolTV.editable=NO;

protocolTV.delegate=self;

NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

//设置text文字居中

paragraph.alignment=NSTextAlignmentCenter;

protocolTV.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);

//设置添加链接部分文字的颜色

protocolTV.linkTextAttributes = @{NSForegroundColorAttributeName:TextBodyColor};

[wechatView addSubview:protocolTV];


NSString *str = @"注册登录即代表同意XX的服务协议和隐私条款";

NSRangeprotocalRange = [strrangeOfString:@"服务协议"];

NSRangeprivacyRange = [strrangeOfString:@"隐私条款"];

NSMutableAttributedString *privacyMutableAttrStr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 12],NSForegroundColorAttributeName:TextBodyColor,NSParagraphStyleAttributeName:paragraph}];


//给需要 点击的部分添加链接

[privacyMutableAttrStr addAttributes:@{NSLinkAttributeName:@"privacy://",NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Semibold" size: 12],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:privacyRange];

[privacyMutableAttrStr addAttributes:@{NSLinkAttributeName:@"protocal://",NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Semibold" size: 12],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:protocalRange];

protocolTV.attributedText= privacyMutableAttrStr;

[protocolTV mas_makeConstraints:^(MASConstraintMaker*make) {

           make.top.equalTo(self.wechatBtn.mas_bottom).offset(24);

           make.centerX.equalTo(bgview.mas_centerX);

           make.left.right.equalTo(bgview);

           make.height.mas_equalTo(15);

           make.width.equalTo(bgview.mas_width);

 }];


#pragma mark - UITextViewDelegate

-(BOOL)textView:(UITextView*)textViewshouldInteractWithURL:(NSURL*)URLinRange:(NSRange)characterRangeinteraction:(UITextItemInteraction)interaction{

    if ([URL.scheme isEqualToString:@"privacy"]) {

//这里调用方法跳到隐私条款页面

        returnNO;

    }

    if ([URL.scheme isEqualToString:@"protocal"]) {

//这里调用方法跳到服务协议页面

        returnNO;

    }

    return YES;

}



其中也有遇到问题:UITextView文本内容居中

需要为文本设置NSMutableParagraphStyle

NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

//设置text文字居中

paragraph.alignment=NSTextAlignmentCenter;

再设置NSMutableAttributedString

NSParagraphStyleAttributeName:paragraph    设置段落样式

此解决方案来自iOS — NSMutableAttributedString和NSMutableParagraphStyle

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 7,198评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 5,835评论 0 1
  • 字体属性设置示例:if(color ==nil) {color = [NSColor redColor];}NSF...
    袏扌戒指阅读 12,643评论 0 2
  • 一、关于AttributedString 在iOS开发过程中系统简单的封装工具已经不够我们使用,为了更好的开发,苹...
    iceMaple阅读 11,882评论 2 31
  • 与NSString类似,在iOS中AttributedString也分为NSAttributedString和 N...
    钱十六阅读 4,296评论 0 0

友情链接更多精彩内容