iOS开发中,“用户使用协议”等可点击文本实现

登录界面经常使用 “用户协议”,“安全使用说明”等链接文本说明,下面是实现方式。

使用UITextView,在文本中加入链接,并设置连接的颜色等

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"点击登录,即表示您同意《用户使用协议》"];
    //设置行间距以及字体大小、颜色
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 4;// 字体的行间距
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0],
                                 NSForegroundColorAttributeName:[UIColor colorWithHex:@"CFCFCF"],
                                 NSParagraphStyleAttributeName:paragraphStyle};
    [attributedString setAttributes:attributes range:NSMakeRange(0, attributedString.length)];
    
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"action://"
                             range:[[attributedString string] rangeOfString:@"《用户使用协议》"]];
    NSDictionary *linkAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHex:@"333333"]};
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(22,100, CGRectGetWidth(self.view.frame)-44, 65)];
    textView.backgroundColor = [UIColor clearColor];
    textView.linkTextAttributes = linkAttributes;
    textView.attributedText = attributedString;
    textView.scrollEnabled = NO;
    textView.font = [UIFont systemFontOfSize:12.0];
    textView.textAlignment = NSTextAlignmentCenter;
    textView.editable = NO;
    textView.delegate = self;
    [self.view addSubview:textView];

实现代理方法,监听点击“《用户使用协议》”点击事件

#pragma mark -
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    //《用户使用协议》
    if ([URL.absoluteString isEqualToString:@"action://"])
    {
        
        return NO;
    }
    return YES;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值