zoukankan      html  css  js  c++  java
  • 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 晚

  • 相关阅读:
    作男人 一定要有品位
    如何管理“人”
    Facebook怎样开发软件:工程师驱动的文化(转)
    为人处事100条——修身养性,经典收藏!
    抽空看看这些电影
    曹重英:技术人员也要打造人脉竞争力(转)
    动态分段统计SQL
    不成熟男人与成熟男人的区别
    Ubuntu11.10国内更新源地址汇总以及添加方法(目前最全最快的源)
    ubuntu11.10 64bits机器安装flash方法
  • 原文地址:https://www.cnblogs.com/TheYouth/p/7967629.html
Copyright © 2011-2022 走看看