- (void)testUrl{
// 测试文本
NSString *text = @"发呆发https://blog.youkuaiyun.com/a18339063397呆发的负担发的分身乏术对方的负担https://www.baidu.com大家看https://www.baidu.com打开房间打开肌肤打开链接附近的空间卡就开个房间卡加客服即可打开附近的卡夫卡的,发呆发https://blog.youkuaiyun.com/a18339063397呆发的负担发的分身乏术对方的负担https://www.baidu.com大家看https://www.baidu.com打开房间打开肌肤打开链接附近的空间卡就开个房间卡加客服即可打开附近的卡夫卡的,发呆发https://blog.youkuaiyun.com/a18339063397呆发的负担发的分身乏术对方的负担https://www.baidu.com大家看https://www.baidu.com打开房间打开肌肤打开链接附近的空间卡就开个房间卡加客服即可打开附近的卡夫卡的。";
// 转成可变属性字符串
NSMutableAttributedString * mAttributedString = [NSMutableAttributedString new];
// 调整行间距段落间距
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
//设置文字两端对齐
paragraphStyle.alignment = NSTextAlignmentJustified;
[paragraphStyle setLineSpacing:2];
[paragraphStyle setParagraphSpacing:4];
UIFont *font = [UIFont systemFontOfSize:15];
// 设置文本属性
NSDictionary *attri = [NSDictionary dictionaryWithObjects:@[font, [UIColor blackColor], paragraphStyle] forKeys:@[NSFontAttributeName, NSForegroundColorAttributeName, NSParagraphStyleAttributeName]];
[mAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:text 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:&error];
if (!regex) {
NSLog(@"正则创建失败error!= %@", [error 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 blueColor];
YYTextBorder *border = [YYTextBorder new];
border.cornerRadius = 3;
border.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
//设定颜色则点击的时候,点击链接的时候链接颜色不会变
// border.fillColor = [UIColor greenColor];
YYTextHighlight *highlight = [YYTextHighlight new];
[highlight setBorder:border];
[highlight setColor:[UIColor redColor]];
[one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
// 根据range替换字符串
[mAttributedString replaceCharactersInRange:match.range withAttributedString:one];
}
}
// 使用YYLabel显示
YYLabel *label = [YYLabel new];
label.userInteractionEnabled = YES;
label.backgroundColor = [UIColor cyanColor];
label.numberOfLines = 0;
label.textVerticalAlignment = YYTextVerticalAlignmentTop;
// label.size = CGSizeMake(Screen_Width, 260);
// label.center = CGPointMake(self.view.width / 2 , 250);
//具体高度有内容决定,不会按照这里设定的
label.frame = CGRectMake(0, 100, Screen_Width, 100);
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(Screen_Width, MAXFLOAT)];
YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text: mAttributedString];
label.height = textLayout.textBoundingSize.height;
NSLog(@"%f",label.height);
}