哈喽,我是二西,今天来介绍下YYLabel,YYTextView的几个强大的用法,时至今日,YYText虽然说作者已经不维护了,但是貌似也没啥能替代的库,它的功能依旧很强大,有问题自己改改就行了不是嘛,唉,希望作者早日康复。
大家有啥更好用的替代库可以评论区告诉我呀~感谢
Butter-Fly music.163.com/#/song?id=4…
YYLabel末尾添加全文、更多、详情等
YYLabel *contentL = [[YYLabel alloc] init];
contentL.font = [UIFont systemFontOfSize:15];
contentL.numberOfLines = 3;
contentL.preferredMaxLayoutWidth = 300;//最大宽度
[self.view addSubview:contentL];
[contentL mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.left.mas_equalTo(16);
make.width.mas_equalTo(300);
}];
NSString *str = @"好想化做一只蝴蝶,乘着微风振翅高飞,现在马上,只想赶快和你见面,烦心的事放在一边,如果忘记那也无所谓,已经没有,多余时间可以浪费,似乎有,什么事会在这片晴空下出现";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
attrStr.yy_font = contentL.font;
contentL.attributedText = attrStr;
//添加详情 不需要点击事件
NSMutableAttributedString *moreAttrStr = [[NSMutableAttributedString alloc] initWithString:@"...详情"];
[moreAttrStr yy_setColor:rgb(71, 154, 248) range:[moreAttrStr.string rangeOfString:@"详情"]];
moreAttrStr.yy_font = contentL.font;
contentL.truncationToken = moreAttrStr;
//添加详情 需要点击事件
NSMutableAttributedString *moreAttrStr = [[NSMutableAttributedString alloc] initWithString:@"...详情"];
moreAttrStr.yy_font = contentL.font;
[moreAttrStr yy_setTextHighlightRange:[moreAttrStr.string rangeOfString:@"详情"] color:rgb(71, 154, 248) backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"点击了详情");
}];
YYLabel *moreL = [[YYLabel alloc] init];
moreL.attributedText= moreAttrStr;
[moreL sizeToFit];
NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:moreL contentMode:UIViewContentModeCenter attachmentSize:moreL.frame.size alignToFont:contentL.font alignment:YYTextVerticalAlignmentCenter];
contentL.truncationToken= truncationToken;
注意下需要点击事件的写法,如果不新建YYLabel做载体,是无法响应点击事件的
拓展用法
有时候文案会在中间换行,这时候末尾的详情就会紧贴着,并不能停靠在最后一行的末尾,有些设计喜欢放在最后面,这里提供一个方案,你们有其他更好的方法可以在评论区告诉我鸭
方法是这样,把truncationToken的“详情”文案设置成透明的,然后自己在最后一行末尾添加一个label或button来显示,这样既可以实现文案在中间换行的时候,“详情”显示在最后,或者文案在最后换行的时候,“详情”不会挡住内容。
上代码:
NSMutableAttributedString *moreAttrStr = [[NSMutableAttributedString alloc] initWithString:@"...详情"];
[moreAttrStr yy_setColor:[UIColor clearColor] range:[moreAttrStr.string rangeOfString:@"详情"]];
moreAttrStr.yy_font = contentL.font;
contentL.truncationToken = moreAttrStr;
NSMutableAttributedString *copyAttrStr = [[NSMutableAttributedString alloc] initWithString:@"详情"];
copyAttrStr.yy_color = rgb(71, 154, 248);
copyAttrStr.yy_font = contentL.font;
YYLabel *moreL = [[YYLabel alloc] init];
moreL.attributedText= copyAttrStr;
[contentL addSubview:moreL];
[moreL mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(0);//位置自己适当调整哈
make.bottom.mas_equalTo(0);
}];
效果如下:
YYLabel添加表情图片标签自定义视图等
//置顶标签
UILabel *topL = [[UILabel alloc] init];
topL.font = [UIFont systemFontOfSize:11];
topL.text = @"置顶";
topL.backgroundColor = [UIColor redColor];
topL.textColor = [UIColor whiteColor];
topL.layer.cornerRadius = 4;
topL.layer.masksToBounds = YES;
topL.textAlignment = NSTextAlignmentCenter;
topL.frame = CGRectMake(0, 0, 32, 16);//设置大小
NSAttributedString *topAttr = [NSAttributedString yy_attachmentStringWithContent:topL contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(topL.frame.size.width+4, topL.frame.size.height) alignToFont:contentL.font alignment:YYTextVerticalAlignmentCenter];
[attrStr insertAttributedString:topAttr atIndex:0];
//图片、表情
UIImage *image = [UIImage imageNamed:@"app_icon_fire_17"];
UIImageView *imageV = [[UIImageView alloc] init];
imageV.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageV.image = image;
NSAttributedString *imageAttr = [NSAttributedString yy_attachmentStringWithContent:imageV contentMode:UIViewContentModeScaleAspectFit attachmentSize:image.size alignToFont:contentL.font alignment:YYTextVerticalAlignmentCenter];
[attrStr appendAttributedString:imageAttr];
contentL.attributedText = attrStr;
使用
YYTextAttachment可以将UIView直接加到富文本里面,可以很方便的用来显示图片啊,表情啊,标签啊,甚至你可以自定义内容然后贴上去,记得设置下大小就行。附带,可以使用YYAnimatedImageView来显示动图
YYTextView添加@、链接,当做整体
YYTextView *contentV = [[YYTextView alloc] init];
[self.view addSubview:contentV];
[contentV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.left.mas_equalTo(16);
make.right.mas_equalTo(-16);
make.height.mas_equalTo(300);
}];
NSMutableAttributedString *allAttrStr = [[NSMutableAttributedString alloc] init];
NSArray *arr = @[@"@亚古兽",@"@加布兽",@"@比丘兽",@"@甲虫兽",@"@巴鲁兽",@"@哥玛兽",@"@巴达兽",@"@迪路兽"];
for (NSString *str in arr) {
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
attrStr.yy_font = [UIFont systemFontOfSize:14];
[attrStr yy_setTextBinding:[YYTextBinding bindingWithDeleteConfirm:NO] range:attrStr.yy_rangeOfAll];//没有删除确认直接删除
[allAttrStr appendAttributedString:attrStr];
[allAttrStr yy_appendString:@" "];
}
NSMutableAttributedString *linkAttrStr = [[NSMutableAttributedString alloc] initWithString:@"https://juejin.cn"];
linkAttrStr.yy_font = [UIFont systemFontOfSize:14];
linkAttrStr.yy_color = [UIColor blueColor];
[linkAttrStr yy_setTextBinding:[YYTextBinding bindingWithDeleteConfirm:YES] range:linkAttrStr.yy_rangeOfAll];//有删除确认
[allAttrStr appendAttributedString:linkAttrStr];
contentV.attributedText = allAttrStr;
使用
YYTextBinding可以把你想要的部分连成一个整体,选择的时候会整块选择,删除的时候会整块删掉,是不是很方便鸭。附带下,要匹配@或者链接,可以使用正则表达式NSRegularExpression来匹配
YYTextSimpleEmoticonParser简单的表情解析
NSMutableDictionary *mapper = [NSMutableDictionary new];
mapper[@":smile:"] = [UIImage imageNamed:@"smile"];
mapper[@":cry:"] = [UIImage imageNamed:@"cry"];
YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new];
parser.emoticonMapper = mapper;
YYLabel *contentL = [[YYLabel alloc] init];
contentL.font = [UIFont systemFontOfSize:14];
contentL.text = @"笑一个:smile:,哭一个:cry:。";
[self.view addSubview:contentL];
[contentL mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.left.mas_equalTo(16);
make.width.mas_equalTo(300);
}];
最后补充
高亮点击啥的基本方法就不介绍了啊,哦,顺便说下,YYTextHighlight有个属性userInfo字典,可以自己添加一些字段,方便在点击的时候传递。
YYTextHighlight *highlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor lightGrayColor]];
highlight.userInfo = @{@"uid":uid};
[attrStr yy_setTextHighlight:highlight range:NSMakeRange(0, str.length)];
contentL.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
__strong typeof(weakSelf) strongSelf = weakSelf;
YYTextHighlight *highlight = [text yy_attribute:YYTextHighlightAttributeName atIndex:range.location]
if (highlight) {
// 点击了高亮文本
NSDictionary *userInfo = highlight.userInfo;
if(userInfo[@"uid"]){
}
}else{
// 点击了普通文本
}
};
比较能用到的暂时只想到这么多,大家如果有其他常用的可以评论区告诉我鸭~~~