通过YYtext实现文本点击(类似微博效果)

来源:互联网 发布:阿里云销售好做吗 编辑:程序博客网 时间:2024/06/15 13:00


最近想在文本中实现其中一段文字的点击事件,进行超链接,自己琢磨了很久,很遗憾最后还是没有通过自己实现出来,在网上查找一番,发现YYtext这个三方是专门为文字类所使用,经过一番了解,发现其中正好有我所需要的,很简单


导入YYtext后 直接调用即可


代码如下:

NSMutableAttributedString *text = [[NSMutableAttributedStringalloc] initWithString:@"Some Text, blabla..."];

    text.yy_font = [UIFontboldSystemFontOfSize:13.0f];

    text.yy_color = [UIColorblueColor];

    [text yy_setColor:[UIColorredColor] range:NSMakeRange(0,4)];


    

    

    [text yy_setTextHighlightRange:NSMakeRange(0,4)//设置点击的位置

                                 color:[UIColororangeColor]

                       backgroundColor:[UIColorwhiteColor]

                             tapAction:^(UIView *containerView,NSAttributedString *text,NSRange range, CGRect rect){

                                 NSLog(@"这里是点击事件");

                             }];

    

    YYLabel *heightRangeLabel = [YYLabelnew];

    heightRangeLabel.frame =CGRectMake(100,250, 160,25);

    heightRangeLabel.attributedText = text;

    heightRangeLabel.userInteractionEnabled =YES;

    heightRangeLabel.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:heightRangeLabel];





0 0
原创粉丝点击