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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 @   TheYouth  阅读(3501)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示