yy_model及 YYLabel
一, yy_model
1.yy_model 可以存放包含数组的属性,调用方法如下:
+ (NSDictionary *)modelCustomPropertyMapper {
return @{@"girlid" : @"id",
@"gnumber" : @"info.gnumber",
@"name" : @"info.name",
@"icon" : @"info.icon",
@"des" : @"info.des",
@"arrayGiftData": @"gift"};
}
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{@"arrayGiftData" : [GoddessGiftData class]};
}
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
if( !ISDICTIONARY(dic) ) return NO;
if (!ISSTRING(_url) || [_url isEqualToString:@""]) return NO;
return YES;
}
@class Shadow, Border, Attachment; @interface Attributes
@property (nonatomic,copy) NSString *name;
@property (nonatomic,strong) NSArray<Shadow*>* shadows; //Array<Shadow>
@property (nonatomic,strong)NSSet *borders; //Set<Border>
@property (nonatomic,strong)NSMutableDictionary *attachments; //Dict<NSString,Attachment>
@end @implementation Attributes
// 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{@"shadows" : [Shadow class],
@"borders" : Border.class,
@"attachments" : @"Attachment" };
}
@end
二, YYLabel
1.在使用 YYLabel 时,一定要给该对象设置背景颜色,不然无法显示.
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Shadow"];
one.yy_font = [UIFont boldSystemFontOfSize:];
one.yy_color = [UIColor whiteColor]; YYLabel *label = [YYLabel new];
label.attributedText = one;
label.frame = CGRectMake(, , , );
// 以下语句很重要,不要漏掉
label.backgroundColor = [UIColor colorWithWhite:0.933 alpha:1.000];
[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 属性值了....一定要有一个勇于怀疑的心,多尝试!!!)
YYLabel *infoLabel = [[YYLabel alloc] init];
[middleView addSubview:infoLabel];
_briefContentLabel = infoLabel;
[infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(seperateLine.mas_bottom).offset(cInfoViewTop);
make.height.mas_equalTo();
make.left.right.mas_equalTo(seperateLine);
}];
// 创建 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个点,参考https://www.jianshu.com/p/0b7759d63074
- (CGFloat)heightOfInfoText:(NSString *)str
{
YYTextLayout *layout = [self createTextLayout:str];
CGFloat infoHeight = layout.textBoundingSize.height + 10 ;
return infoHeight;
}
或者使用不同属性的字符串
#pragma mark 设置特殊的文字和页面高度
- (NSMutableAttributedString *)getCardSuccMsgAttrStr{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"在接下来的30分钟内,主播收到的里程值会全部双倍"];
attr.yy_font = [[PTVConfig instance] normalFont: ];
attr.yy_color = MAKECOLOR(0x9B, 0x9B, 0x9B);
[attr yy_setColor:UIColorFromRGB(0xD2800C) range:NSMakeRange(, )];
attr.yy_lineSpacing = ;
attr.yy_alignment = NSTextAlignmentCenter;
return attr;
} - (NSMutableAttributedString *)getPrestigeSuccMsgAttrStr{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"主播声望值+1"];
attr.yy_font = [[PTVConfig instance] normalFont: ];
attr.yy_color = MAKECOLOR(0x9B, 0x9B, 0x9B);
[attr yy_setColor:UIColorFromRGB(0xD2800C) range:NSMakeRange(, )];
attr.yy_lineSpacing = ;
attr.yy_alignment = NSTextAlignmentCenter;
return attr;
} - (NSMutableAttributedString *)getFailMsg:(NSString *)msg{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:msg];
attr.yy_font = [[PTVConfig instance] normalFont: ];
attr.yy_color = MAKECOLOR(0x9B, 0x9B, 0x9B);
attr.yy_lineSpacing = ;
attr.yy_alignment = NSTextAlignmentCenter;
return attr;
} // 调用方式
- (void)updateLabelText:(ToastMsgType)type errmsg:(NSString *)errmsg
{
NSMutableAttributedString *attri = [NSMutableAttributedString new];
switch (type) {
case :
attri = [self getCardSuccMsgAttrStr];
break; case :
attri = [self getPrestigeSuccMsgAttrStr];
break; case :
attri = [self getFailMsg:errmsg];
break;
}
// 设置内容
self.subtitleLabel.textLayout = [self createTextLayout:attri]; CGFloat labelHeight = [self heightOfInfoText:attri]; [self mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(labelHeight);
}];
} // 创建 textLayout
- (YYTextLayout*)createTextLayout:(NSMutableAttributedString *)attr
{
CGFloat maxWidth = subtitleMaxWidth ;
CGSize containerSize = CGSizeMake(maxWidth , CGFLOAT_MAX);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:containerSize text:attr];
return layout;
} // 高度往往有10个点的误差,需要比 YYLayout 计算的高度多10个点
- (CGFloat)heightOfInfoText:(NSMutableAttributedString *)attr
{
YYTextLayout *layout = [self createTextLayout:attr];
CGFloat infoHeight = layout.textBoundingSize.height + [self getTopBottomMargin] ;
return infoHeight;
}
或者使用container的方式
YYTextContainer *container = [YYTextContainer containerWithSize:containerSize];
container.maximumNumberOfRows = ;
container.truncationType = YYTextTruncationTypeEnd;
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:attrM];
yy_model及 YYLabel的更多相关文章
- YYLabel 自动布局 富文本文字点击事件
YYLabel显示多行除了需要设置numberOfLines = 0以外,还需要设置preferredMaxLayoutWidth最大的宽度值才可以生效多行效果 YYLabel中的NSMutableA ...
- 利用YYLabel 进行图文混排+高度计算
利用YYLabel 进行图文混排+高度计算 1.项目需求: 用一个控件显示图片和文字,并且依据图片和文字动态计算控件的高度. 2.方案: 利用YYLabel控件和属性字符串处理. 注:(在使用YYLa ...
- YYLabel计算富文本高度-膜拜大神
http://www.jianshu.com/p/07cd655fee7e YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:C ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- IOS开发基础知识--碎片50
1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...
- YYText-显示富文本
github地址: https://github.com/ibireme/YYText CocoaPods安装: pod 'YYText' 1.YYLabel使用注意 private lazy var ...
- iOS高仿app源码:纯代码打造高仿优质《内涵段子》
iOS高仿app源码:纯代码打造高仿优质<内涵段子>收藏下来 字数1950 阅读4999 评论173 喜欢133 Github 地址 https://github.com/Charlesy ...
- YYKit之YYText
原文:http://www.cnblogs.com/lujianwenance/p/5716804.html 本文的目的是希望能帮助到我们更快的熟悉和学习YYText的结构和实现的思路,如有不正确 ...
- iOS开发常用第三方开源框架 持续更新中...
键盘管理 TPKeyboardAvoiding IQKeyboardManager(1.2.8) 弹窗HUD MBProgressHUD(0.9.2) SVProgressHUD UIView+Toa ...
随机推荐
- 线程安全的无锁RingBuffer的实现
这里的线程安全,是指一个读线程和一个写线程,读写两个线程是安全的,而不是说多个读线程和多个写线程是安全的.. 在程序设计中,我们有时会遇到这样的情况,一个线程将数据写到一个buffer中,另外一个线程 ...
- QT C1041错误
使用QT Createor + MSVC2013写程序时.我复制了一份之前OK的代码到新的工程中,工程名字和之前的工程一样,出现了奇怪的错误.开始程序总是崩溃,后来我删掉shadow build目录, ...
- java ssh 框架下 利用junit4 spring-test进行单元测试
ssh框架下 由于bean实列 都交给spring 管理,要做单元测试就比较苦难,junit4 引入注解方便很多: 1. 加入依赖包 使用Spring的测试框架需要加入以下依赖包: JUnit 4 ...
- 2016-03-04记录 H264.TXT 转成 H264.h264
H264.TXT文件 来源于板子上串口输出的数据,需要把该数据转成 *.h264用 H264的软件打开观察 txt中数据截图如下: MATLAB读入数据的代码: clc;close all;clear ...
- [NHibernate]一对多关系(关联查询)
目录 写在前面 文档与系列文章 一对多查询 总结 写在前面 上篇文章介绍了nhibernate的一对多关系如何配置,以及级联删除,级联添加数据的内容.这篇文章我们将学习nhibernate中的一对多关 ...
- 莫比乌斯环-vtkTriangleStrip
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- Eclipse快捷键与使用技巧总结
快捷键 功能 Ctrl + / 双斜杠注释 Ctrl + Shilft + / 块注释 Alt + / 代码提示 Ctrl + I (或 Ctrl + Shift + F) 格式化代码 Ctrl + ...
- saltstack(master迁移)
环境:http://www.cnblogs.com/zzzhfo/p/6126223.html 在添加一台master 把master的/etc/salt目录下的pki打包 上传至第二台master的 ...
- php函数parse_url
1.需求 了解parse_url的使用方法 2.实例 $uri = parse_url('http://dummy'.$_SERVER['REQUEST_URI']); var_dump($uri); ...
- 【Python基础学习一】在OSX系统下搭建Python语言集成开发环境 附激活码
Python是一门简单易学,功能强大的编程语言.它具有高效的高级数据结构和简单而有效的面向对象编程方法.Python优雅的语法和动态类型以及其解释性的性质,使它在许多领域和大多数平台成为编写脚本和快速 ...