iOS开发UI篇 —— TextView特殊文字链接(超链接)

NSString *readMessage = @"在...注册并创建账户的同时,我接受服务条款和隐私条款";

UITextView *textView = [[UITextView alloc] init];
textView.textColor = [UIColor blackColor];
textView.font = [UIFont  systemFontOfSize:14];
NSAttributedString *attri = [NSMutableAttributedString attributedStringWithMessage:readMessage                                                                             paragraphSpacing:0                                                                                      lineSpacing:0                                                                                        firstStr:@"服务条款"                                                                                  secendStr:@"隐私条款"];
 textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor]}; // 修改可点击文字的颜色
textView.attributedText = attri;
textView.delegate = self;
[self.reginView addSubview:textView];
textView.backgroundColor = [UIColor clearColor];
textView.editable = NO;
[self.view addSubView: textView];
//代理:
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    NSRange range = [self.readMessage rangeOfString:@"服务条款"];//
    if (characterRange.location == range.location) {//位置是否相同
//跳转的控制器或页面
    //    UserFileVC *userFileVC = [[UserFileVC  alloc] init];
     //   [self presentViewController:userFileVC animated:YES completion:nil];
        return NO; //这里必须返回,否则会出现长按崩溃的bug
    }else {
        NSRange otherRange = [self.readMessage rangeOfString:@"隐私条款"];
        if (characterRange.location == otherRange.location) {
//跳转的控制器或页面
       //     UserFileVC *userFileVC = [[UserFileVC  alloc] init];
         //   [self presentViewController:userFileVC animated:YES completion:nil];
        }
        return NO; //这里必须返回,否则会出现长按崩溃的bug
    }
    return YES;
}

//分类方法如下:(一般写在分类方法中)
+ (NSAttributedString *)attributedStringWithMessage:(NSString *)message paragraphSpacing:(CGFloat)spacing lineSpacing:(CGFloat)lineSpace firstStr:(NSString *)firstStr secendStr:(NSString *)secendStr{
    
    // 设置属性
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    // 设置行间距
    paragraphStyle.paragraphSpacing = spacing; // 段落间距
    paragraphStyle.lineSpacing = lineSpace;      // 行间距
    NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName:[UIColor blackColor],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:message attributes:attributes];
    [attrStr addAttributes:@{
                             NSLinkAttributeName:firstStr
                             }
                     range:[message rangeOfString:firstStr]];
    [attrStr addAttributes:@{
                             NSLinkAttributeName:secendStr
                             }
                     range:[message rangeOfString:secendStr]];
    return attrStr;
}

  上面的代码还有个bug还未解决,就是只要长按那个textView控件就去触发这个方法(超链接),待之后想到后再更新。

                                                              2017.12.3 晚

posted @ 2017-12-03 22:15  TheYouth  阅读(3485)  评论(0编辑  收藏  举报