yy_model及 YYLabel
一, yy_model
1.yy_model 可以存放包含数组的属性,调用方法如下:
1 + (NSDictionary *)modelCustomPropertyMapper { 2 return @{@"girlid" : @"id", 3 @"gnumber" : @"info.gnumber", 4 @"name" : @"info.name", 5 @"icon" : @"info.icon", 6 @"des" : @"info.des", 7 @"arrayGiftData": @"gift"}; 8 } 9 10 + (NSDictionary *)modelContainerPropertyGenericClass { 11 return @{@"arrayGiftData" : [GoddessGiftData class]}; 12 } 13 - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic { 14 if( !ISDICTIONARY(dic) ) return NO; 15 if (!ISSTRING(_url) || [_url isEqualToString:@""]) return NO; 16 return YES; 17 }
1 @class Shadow, Border, Attachment; 2 3 @interface Attributes 4 @property (nonatomic,copy) NSString *name; 5 @property (nonatomic,strong) NSArray<Shadow*>* shadows; //Array<Shadow> 6 @property (nonatomic,strong)NSSet *borders; //Set<Border> 7 @property (nonatomic,strong)NSMutableDictionary *attachments; //Dict<NSString,Attachment> 8 @end 9 10 @implementation Attributes 11 // 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。 12 + (NSDictionary *)modelContainerPropertyGenericClass { 13 return @{@"shadows" : [Shadow class], 14 @"borders" : Border.class, 15 @"attachments" : @"Attachment" }; 16 } 17 @end
二, YYLabel
1.在使用 YYLabel 时,一定要给该对象设置背景颜色,不然无法显示.
1 NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Shadow"]; 2 one.yy_font = [UIFont boldSystemFontOfSize:30]; 3 one.yy_color = [UIColor whiteColor]; 4 5 YYLabel *label = [YYLabel new]; 6 label.attributedText = one; 7 label.frame = CGRectMake(100, 100, 100, 100); 8 // 以下语句很重要,不要漏掉 9 label.backgroundColor = [UIColor colorWithWhite:0.933 alpha:1.000]; 10 [self.view addSubview:label];
2.在 cell 中使用 YYLabel 时,cell 重用时,其中的 YYLabel 视图有可能会被清除.....因此需要在复用 cell 或创建 cell 后,判断 YYLabel 视图是否存在:若不存在,就创建.
3.YYLabel 视图的 attributedText属性,和普通 UILabel 的 attributedText 属性,基本相同,赋值方法均为_chatMsgLabel.attributedText = attrStr;而且都可以直接使用 NSAttributedString 对象的 appendAttributedString 属性/ addAttribute 属性.
(PS:使用yy_attachmentStringWithContent:...时,就不要使用 addAttribute 属性了,)
YYLabel 视图一个很大的便捷是:使用
NSMutableAttributedString 对象的拓展 yy_color 和 yy_setColor:range: 属性,快速设置文字的属性,代替了复杂的addAttribute 属性.
唯一不同的一点就是:YYLabel无法识别 NSAttributedString 类的 attributedStringWithAttachment 方法添加的图片.YYLabel 添加图片需要 NSAttributedString的拓展类方法:yy_attachmentStringWithContent:....
4.在cell 中调用YYLabel时,可以用其对象的 textLayout 属性来代替 attributedText 给YYLabel赋值,同时使用 textLayout 属性的 textBoundingSize 属性
来设置 YYLabel 的宽高.代码如下:
(PS:设置YYLabel 的宽高时,尽量比 YYTextLayout 对象的 textBoundingSize 属性大,不然显示不全文字.....一开始我也质疑该属性是否准确,但是通过看层次结构图之后,就确定是我需要加大 textBoundingSize 属性值了....一定要有一个勇于怀疑的心,多尝试!!!)
1 YYLabel *infoLabel = [[YYLabel alloc] init]; 2 [middleView addSubview:infoLabel]; 3 _briefContentLabel = infoLabel; 4 [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) { 5 make.top.mas_equalTo(seperateLine.mas_bottom).offset(cInfoViewTop); 6 make.height.mas_equalTo(10); 7 make.left.right.mas_equalTo(seperateLine); 8 }];
// 创建 textLayout
- (YYTextLayout*)createTextLayout:(NSString *)newText
{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:newText];
attr.yy_font = [[PTVConfig instance] normalFont:10 ];
attr.yy_color = MAKECOLOR(0x9B, 0x9B, 0x9B);
attr.yy_lineSpacing = 5;
attr.yy_alignment = NSTextAlignmentCenter;
CGFloat maxWidth = subtitleMaxWidth ;
CGSize containerSize = CGSizeMake(maxWidth , CGFLOAT_MAX);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:containerSize text:attr];
return layout;
}
// 调用方式
- (void)updateLabelText:(NSString *)str
{
// 设置内容
self.subtitleLabel.textLayout = [self createTextLayout:str];
CGFloat labelHeight = [self heightOfInfoText:str];
[self.subtitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(labelHeight);
}];
}
// 高度往往有10个点的误差,需要比 YYLayout 计算的高度多10个点
- (CGFloat)heightOfInfoText:(NSString *)str
{
YYTextLayout *layout = [self createTextLayout:str];
CGFloat infoHeight = layout.textBoundingSize.height + 10 ;
return infoHeight;
}
或者使用不同属性的字符串
1 #pragma mark 设置特殊的文字和页面高度 2 - (NSMutableAttributedString *)getCardSuccMsgAttrStr{ 3 NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"在接下来的30分钟内,主播收到的里程值会全部双倍"]; 4 attr.yy_font = [[PTVConfig instance] normalFont:10 ]; 5 attr.yy_color = MAKECOLOR(0x9B, 0x9B, 0x9B); 6 [attr yy_setColor:UIColorFromRGB(0xD2800C) range:NSMakeRange(5, 4)]; 7 attr.yy_lineSpacing = 5; 8 attr.yy_alignment = NSTextAlignmentCenter; 9 return attr; 10 } 11 12 - (NSMutableAttributedString *)getPrestigeSuccMsgAttrStr{ 13 NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"主播声望值+1"]; 14 attr.yy_font = [[PTVConfig instance] normalFont:10 ]; 15 attr.yy_color = MAKECOLOR(0x9B, 0x9B, 0x9B); 16 [attr yy_setColor:UIColorFromRGB(0xD2800C) range:NSMakeRange(5, 2)]; 17 attr.yy_lineSpacing = 5; 18 attr.yy_alignment = NSTextAlignmentCenter; 19 return attr; 20 } 21 22 - (NSMutableAttributedString *)getFailMsg:(NSString *)msg{ 23 NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:msg]; 24 attr.yy_font = [[PTVConfig instance] normalFont:10 ]; 25 attr.yy_color = MAKECOLOR(0x9B, 0x9B, 0x9B); 26 attr.yy_lineSpacing = 5; 27 attr.yy_alignment = NSTextAlignmentCenter; 28 return attr; 29 } 30 31 32 // 调用方式 33 - (void)updateLabelText:(ToastMsgType)type errmsg:(NSString *)errmsg 34 { 35 NSMutableAttributedString *attri = [NSMutableAttributedString new]; 36 switch (type) { 37 case 1: 38 attri = [self getCardSuccMsgAttrStr]; 39 break; 40 41 case 2: 42 attri = [self getPrestigeSuccMsgAttrStr]; 43 break; 44 45 case 3: 46 attri = [self getFailMsg:errmsg]; 47 break; 48 } 49 // 设置内容 50 self.subtitleLabel.textLayout = [self createTextLayout:attri]; 51 52 CGFloat labelHeight = [self heightOfInfoText:attri]; 53 54 [self mas_updateConstraints:^(MASConstraintMaker *make) { 55 make.height.mas_equalTo(labelHeight); 56 }]; 57 } 58 59 // 创建 textLayout 60 - (YYTextLayout*)createTextLayout:(NSMutableAttributedString *)attr 61 { 62 CGFloat maxWidth = subtitleMaxWidth ; 63 CGSize containerSize = CGSizeMake(maxWidth , CGFLOAT_MAX); 64 YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:containerSize text:attr]; 65 return layout; 66 } 67 68 // 高度往往有10个点的误差,需要比 YYLayout 计算的高度多10个点 69 - (CGFloat)heightOfInfoText:(NSMutableAttributedString *)attr 70 { 71 YYTextLayout *layout = [self createTextLayout:attr]; 72 CGFloat infoHeight = layout.textBoundingSize.height + [self getTopBottomMargin] ; 73 return infoHeight; 74 }
或者使用container的方式
1 YYTextContainer *container = [YYTextContainer containerWithSize:containerSize]; 2 container.maximumNumberOfRows = 2; 3 container.truncationType = YYTextTruncationTypeEnd; 4 YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:attrM];
转载于:https://www.cnblogs.com/lz465350/p/5942592.html
yy_model及 YYLabel相关推荐
- 使用YYLabel匹配连接
YYLabel用来处理label 中的链接以及匹配文字及图片等很好的框架.文章分享使用storyboard和代码两种方式使用YY. YYLabel是一个UIView,直接拖UIView然后指定它的cl ...
- iOS - UILabel点击选中文字,部分高亮,YYLabel
最近有这个一个小需求,有这样一个tipLabel:"添加QQ:670360112 进行在线咨询" .其中qq号要求高亮并且可以点击复制,并弹出提示框提示复制成功. 好吧,需求基本是 ...
- YYLabel和YYTextView的使用
在开发中,一般都会有遵守服务条款之类的声明,而在这些提示语当中,常常会设置某一段文字可以点击,值得注意的是控制类型必须改成YYLabel class. 1.YYLabel NSMutableAttri ...
- YYLabel的几个实用小技巧
---- 如果你在开发社交相关的APP时,可能会用到富文本超链接等功能,可选的框架很多,YYKit是一个不错的选择,我在使用的过程中遇到几个小麻烦跟大家分享一下(主要是YYLabel,更高级的用法还没 ...
- yylabel html富文本,YYLabel 自动布局 富文本文字点击事件
YYLabel显示多行除了需要设置numberOfLines = 0以外,还需要设置preferredMaxLayoutWidth最大的宽度值才可以生效多行效果 YYLabel中的NSMutableA ...
- YYLabel 点击富文本中的文字
当我们做法律说明和用户协议的时候经常需要点击Label中的文字跳转到不同的页面,使用YYLabel可以实现 下面是Label的初始化,YYLabel,主要是设置userInfo不然区分不出点击的是哪段 ...
- swift创建嵌套模型使用yy_model解析json
依然是使用swift混编,因为现在的项目是oc项目,但是不想写太多oc代码,所以用swift创建模型.因为swift创建模型可以很方便的输入默认值代码量也少,这样在读取json的时候就不用担心,因为j ...
- yylabel 加载html,YYKit是个好东西-YYLabel实现一个文本多个点击事件
NSString *agreementText = @"点击登录即表示已同意并同意<xxx用户协议>与<xxx隐私政策>"; NSMutableAttrib ...
- php两字段一列竖排显示,iOS 10 YYLabel 竖排多列文字只能显示一列
代码片段 YYLabel *textLabel = [[YYLabel alloc] init]; textLabel.numberOfLines = 0; textLabel.origin = CG ...
- iOS14 YYAnimatedImageView图片不显示,多数用在YYLabel的富文本中添加的图片
方案一 简单粗暴 // 直接在YYAnimatedImageView上添加一个UIImageView /// @param yyimage YYAnimatedImageView /// @param ...
最新文章
- Ubuntu里面一些权限问题
- java青蛙青蛙跳井_公务员行测技巧:青蛙跳井问题
- http协议的Request Payload 和 Form Data 的区别
- mysql cluster 视频_mysql cluster 部署实战
- IBatis.Net学习笔记系列文章
- 学习是一个漫长不能松懈的过程
- Trie:hdu 4825、1251、1247、Poj 3764
- CentOS 5安装mplayer
- C#多线程操作界面控件的解决方案
- 【Flink】Error during binlog processing last offset stored = null binglog reader near position
- 二胎妈妈,35岁,年薪70万,家庭工作平衡太难,要不要辞职?
- loss函数之MultiLabelSoftMarginLoss
- 【渝粤教育】广东开放大学 商务翻译实务 形成性考核 (49)
- java 动态创建数据库和动态连接数据库
- Mac上使用exiftool生成XMP文件的方法
- 显示器尺寸对照表_怎样知道自己的电脑显示器是多少寸的
- 异步时钟脉冲同步器的设计
- 登陆邮箱的方法有哪些?解析mail163邮箱如何误删恢复?
- 开源音乐_与这位开源音乐老师一起学习乐器
- Android学习之zygote启动流程
热门文章
- ppt显示无法连接服务器文件路径,ppt打开时显示“此演示文稿包含到其他文件的链接”弹出框是什么原因?...
- python回归算法预测数据_python逻辑回归算法预测
- cv个人计算机SCI英文简历模板,个人英文简历模板
- PyCharm破解版 mac
- 鸟哥linux私房菜pdf及配套视频分享
- 文档管理系统mindoc安装
- QNX系统将C/C++代码编译为lib***.so文件
- 支付中心设计与方案,收藏了
- 软件测试面试题(全)
- 惠普打印机服务器状态未知,惠普打印机状态显示需要注意