• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

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

开发技术 开发技术 3小时前 1次浏览

  最近在项目开发中遇到了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.comn”;

    NSString *thirdStr=@”或n”;

    NSString *fourthStr=@”rjr@cloudcc.comn”;

    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.comn”];

            if (([url isKindOfClass:[NSString class]] && [(NSString *)url containsString:@”firstmanager”])) {

                range = [self.textRemind.text rangeOfString:@”guanshuai@cloudcc.comn”];

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

                range = [self.textRemind.text rangeOfString:@”rjr@cloudcc.comn”];

            }

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

            

          }

            

        }

}

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

    if ([(NSString *)URL containsString:@”firstmanager”]) {

        

        return NO;

    } else if ([(NSString *)URL containsString:@”secondmanager”]) {

        

        return NO;

    }

    

    return YES;

}


程序员灯塔
转载请注明原文链接:iOS中使用UITextView设置不同文本部分点击事件小结
喜欢 (0)