您的位置:首页 > 其它

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

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

导入YYtext后 直接调用即可

代码如下:

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

    text.yy_font = [UIFont
boldSystemFontOfSize:13.0f];

    text.yy_color = [UIColor
blueColor];

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

    

    

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

                                 color:[UIColor
orangeColor]

                       backgroundColor:[UIColor
whiteColor]

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

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

                             }];

    

    YYLabel *heightRangeLabel = [YYLabel
new];

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

    heightRangeLabel.attributedText = text;

    heightRangeLabel.userInteractionEnabled =
YES;

    heightRangeLabel.backgroundColor = [UIColor
whiteColor];

    [self.view
addSubview:heightRangeLabel];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  YYtext 文字点击