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

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

1.匹配超鏈接
// 測試文本
NSString *text = @"這是一個超鏈接http://www.itdecent.cn/users/c75b8e27dc43這是一個超鏈接";

// 轉(zhuǎn)成可變屬性字符串
    NSMutableAttributedString * mAttributedString = [NSMutableAttributedString new];

// 調(diào)整行間距段落間距
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    [paragraphStyle setLineSpacing:2]; 
    [paragraphStyle setParagraphSpacing:4]; 

// 設(shè)置文本屬性
    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;
// 根據(jù)匹配條件,創(chuàng)建了一個正則表達(dá)式
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&err];
    if (!regex) {
        NSLog(@"正則創(chuàng)建失敗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設(shè)置一些文本屬性
            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];
            // 根據(jù)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;

未完待續(xù) > 有時間繼續(xù)整理自定義表情這塊的實(shí)現(xiàn)...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,050評論 4 61
  • 放學(xué)后 本是沖著抹茶蛋糕去的大拇指 抹茶蛋糕沒買到 卻發(fā)現(xiàn)了新美食 明天試試好不好吃 每天晚修的動力就是回家后一頓...
    豆妹_閱讀 227評論 0 0
  • 以前覺得兩個人在一起,不管遇到什么,只要一起面對,心在一起就什么也不懼了; 現(xiàn)在覺得兩個人在一起,害怕的越來越多,...
    細(xì)柳扶桑閱讀 209評論 0 0
  • 1 Frank老師每次的作業(yè)很妙啊,像是一個真正的領(lǐng)路人,他拿著一盞燈,一步...
    文二妞閱讀 289評論 1 0
  • 我在你來過的地方為你撐著傘 在細(xì)雨綿綿里為你等候 在水花撿起的地方 等你不變的承諾 還有你的溫柔 用我溫暖的雙手輕...
    田萍閱讀 434評論 3 9

友情鏈接更多精彩內(nèi)容