YYText学习(一)使用YYLabel显示链接和自定义表情

由于项目中增加了链接和自定义表情等功能,最近一周都在查找富文本这块的实现,之前的用过的RTLabelTTTAttributedLabel由于实现不是那么方便高度计算不灵活,让我这次想到了YY大神的YYText,使用中觉得非常方便好用,于是记下自己的学习过程。

1.匹配超链接
// 测试文本
NSString *text = @"这是一个超链接//www.greatytc.com/users/c75b8e27dc43这是一个超链接";

// 转成可变属性字符串
    NSMutableAttributedString * mAttributedString = [NSMutableAttributedString new];

// 调整行间距段落间距
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    [paragraphStyle setLineSpacing:2]; 
    [paragraphStyle setParagraphSpacing:4]; 

// 设置文本属性
    NSDictionary *attri = [NSDictionary dictionaryWithObjects:@[font, [UIColor blackColor], paragraphStyle] forKeys:@[NSFontAttributeName, NSForegroundColorAttributeName, NSParagraphStyleAttributeName]];
    [mAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:self attributes:attri]];

// 匹配条件
NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";

NSError *error = NULL;
// 根据匹配条件,创建了一个正则表达式
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&err];
    if (!regex) {
        NSLog(@"正则创建失败error!= %@", [err localizedDescription]);
    } else {
        NSArray *allMatches = [regex matchesInString:mAttributedString.string options:NSMatchingReportCompletion range:NSMakeRange(0, mAttributedString.string.length)];
        for (NSTextCheckingResult *match in allMatches) {
            NSString *substrinsgForMatch2 = [mAttributedString.string substringWithRange:match.range];
            NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:substrinsgForMatch2];
            // 利用YYText设置一些文本属性
            one.yy_font = font;
            one.yy_underlineStyle = NSUnderlineStyleSingle;
            one.yy_color = [UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000];
            
            YYTextBorder *border = [YYTextBorder new];
            border.cornerRadius = 3;
            border.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
            border.fillColor = [UIColor colorWithWhite:0.000 alpha:0.220];
            
            YYTextHighlight *highlight = [YYTextHighlight new];
            [highlight setBorder:border];
            [one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
            // 根据range替换字符串
            [mAttributedString replaceCharactersInRange:match.range withAttributedString:one];
        }
    }

// 使用YYLabel显示
YYLabel *label = [YYLabel new];
    label.userInteractionEnabled = YES;
    label.numberOfLines = 0;
    label.textVerticalAlignment = YYTextVerticalAlignmentTop;
    label.size = CGSizeMake(260, 260);
    label.center = CGPointMake(self.view.width / 2, 200);
    label.attributedText = mAttributedString;
    [self.view addSubview:label];
    label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSString *string = [NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]];
        NSLog(@"%@", string);
    };

// 利用YYTextLayout计算高度
YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(260, MAXFLOAT)];
    YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text: mAttributedString];
    label.height = textLayout.textBoundingSize.height;

未完待续 > 有时间继续整理自定义表情这块的实现...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • 放学后 本是冲着抹茶蛋糕去的大拇指 抹茶蛋糕没买到 却发现了新美食 明天试试好不好吃 每天晚修的动力就是回家后一顿...
    豆妹_阅读 204评论 0 0
  • 以前觉得两个人在一起,不管遇到什么,只要一起面对,心在一起就什么也不惧了; 现在觉得两个人在一起,害怕的越来越多,...
    细柳扶桑阅读 186评论 0 0
  • 1 Frank老师每次的作业很妙啊,像是一个真正的领路人,他拿着一盏灯,一步...
    文二妞阅读 241评论 1 0
  • 我在你来过的地方为你撑着伞 在细雨绵绵里为你等候 在水花捡起的地方 等你不变的承诺 还有你的温柔 用我温暖的双手轻...
    田萍阅读 384评论 3 9