由于项目中增加了链接和自定义表情等功能,最近一周都在查找富文本这块的实现,之前的用过的RTLabel和TTTAttributedLabel由于实现不是那么方便高度计算不灵活,让我这次想到了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;
未完待续 > 有时间继续整理自定义表情这块的实现...