YYText學習(一)使用YYLabel顯示鏈接和自定義表情

由于項目中增加了鏈接和自定義表情等功能,最近一周都在查找富文本這塊的實現,之前的用過的RTLabelTTTAttributedLabel由于實現不是那么方便高度計算不靈活,讓我這次想到了YY大神的YYText,使用中覺得非常方便好用,于是記下自己的學習過程。

1.匹配超鏈接
// 測試文本
NSString *text = @"這是一個超鏈接http://www.lxweimin.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,253評論 4 61
  • 放學后 本是沖著抹茶蛋糕去的大拇指 抹茶蛋糕沒買到 卻發現了新美食 明天試試好不好吃 每天晚修的動力就是回家后一頓...
    豆妹_閱讀 204評論 0 0
  • 以前覺得兩個人在一起,不管遇到什么,只要一起面對,心在一起就什么也不懼了; 現在覺得兩個人在一起,害怕的越來越多,...
    細柳扶桑閱讀 186評論 0 0
  • 1 Frank老師每次的作業很妙啊,像是一個真正的領路人,他拿著一盞燈,一步...
    文二妞閱讀 241評論 1 0
  • 我在你來過的地方為你撐著傘 在細雨綿綿里為你等候 在水花撿起的地方 等你不變的承諾 還有你的溫柔 用我溫暖的雙手輕...
    田萍閱讀 384評論 3 9