其他分享
首页 > 其他分享> > iOS中使用UITextView设置不同文本部分点击事件小结

iOS中使用UITextView设置不同文本部分点击事件小结

作者:互联网

  最近在项目开发中遇到了设置多行文本,点击蓝色邮箱部分跳转到发邮件页面功能。当然比较简单的方式是多标签单独设置,那样稍显麻烦。我们能不能用一个控件,给某一部分添加点击事件,结果是可以的,UITextView完美实现这个功能,代码如下。

  1、添加UITextView的代理UITextViewDelegate

  2、@property (nonatomic, strong) UITextView *textRemind;//设置UITextView属性变量

  

- (UITextView *)textRemind{

    if (_textRemind == nil) {

        _textRemind = [[UITextView alloc]init];

        _textRemind.backgroundColor = [UIColor greenColor];

        _textRemind.textColor=K_CC_COLOR_STRING(@"#999999");

        //_textRemind.textAlignment = NSTextAlignmentRight;//此处没有效果,需要在属性里面单独设置

        _textRemind.font = K_CC_FONT_SEMIBOLD(14);

    }

    return _textRemind;

}

 

//顶部菜单标题

    NSString *firstStr=@"此功能暂时无法使用,请联系\n";

    NSString *secondStr=@"guanshuai@cloudcc.com\n";

    NSString *thirdStr=@"或\n";

    NSString *fourthStr=@"rjr@cloudcc.com\n";

    NSString *fifthStr=@"开通";

    NSString *remindStr=[NSString stringWithFormat:@"%@%@%@%@%@",firstStr,secondStr,thirdStr,fourthStr,fifthStr];

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:remindStr];

    //给需要点击的部分添加关键字

   [attributedString addAttribute:NSLinkAttributeName

                            value:@"firstmanager"

                            range:[[attributedString string] rangeOfString:secondStr]];

   [attributedString addAttribute:NSLinkAttributeName

                            value:@"secondmanager"

                            range:[[attributedString string] rangeOfString:fourthStr]];

    //设置水平中间对齐

    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];

    [paragrahStyle setAlignment:NSTextAlignmentCenter];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, remindStr.length)];

   self.textRemind.attributedText = attributedString;

    //设置点击部分的文字颜色

   self.textRemind.linkTextAttributes = @{NSForegroundColorAttributeName: K_CC_COLOR_STRING(@"#2D6CFC") };

 

   self.textRemind.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘

   self.textRemind.scrollEnabled = NO;

   self.textRemind.selectable = NO;

   self.textRemind.delegate = self;

   UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addGestureRecognizer:)];

   [self.textRemind addGestureRecognizer:tapRecognizer];

    [self.view addSubview:self.textRemind];

    [self.textRemind mas_makeConstraints:^(MASConstraintMaker *make) {

        make.center.equalTo(self.view);

        make.height.mas_equalTo(100);

        make.width.mas_equalTo(K_CC_SCREEN_WIDTH-32);

    }];

3、添加手势处理

-(void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer{

    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])

    {

        CGPoint tapLocation = [gestureRecognizer locationInView:self.textRemind];

        UITextPosition *textPosition = [self.textRemind closestPositionToPoint:tapLocation];

        NSDictionary *attributes = [self.textRemind textStylingAtPosition:textPosition inDirection:UITextStorageDirectionBackward];

        NSURL *url = attributes[NSLinkAttributeName];

        if(url) {

            NSRange range = [self.textRemind.text rangeOfString:@"guanshuai@cloudcc.com\n"];

            if (([url isKindOfClass:[NSString class]] && [(NSString *)url isEqualToString:@"firstmanager"])) {

                range = [self.textRemind.text rangeOfString:@"guanshuai@cloudcc.com\n"];

            } else if(([url isKindOfClass:[NSString class]] && [(NSString *)url isEqualToString:@"secondmanager"])){

                range = [self.textRemind.text rangeOfString:@"rjr@cloudcc.com\n"];

            }

            [self  textView:self.textRemind shouldInteractWithURL:url inRange:range];

            

          }

            

        }

}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{

    if ([(NSString *)URL isEqualToString:@"firstmanager"]) {

        

        return NO;

    } else if ([(NSString *)URL isEqualToString:@"secondmanager"]) {

        

        return NO;

    }

    

    return YES;

}

标签:textRemind,UITextView,self,iOS,range,NSString,小结,attributedString
来源: https://www.cnblogs.com/bigant9527/p/15601887.html