由于項目中增加了鏈接和自定義表情等功能,最近一周都在查找富文本這塊的實現,之前的用過的RTLabel和TTTAttributedLabel由于實現不是那么方便高度計算不靈活,讓我這次想到了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;
未完待續 > 有時間繼續整理自定義表情這塊的實現...