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

{   

    CGFloat labelHeight = [self heightOfInfoText];    

    [self.subtitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {

        make.height.mas_equalTo(labelHeight);

    }];

}

 

// 高度往往有10个点的误差,需要比 YYLayout 计算的高度多10个点,参考https://www.jianshu.com/p/0b7759d63074

- (CGFloat)heightOfInfoText

{

    YYTextLayout *layout = self.contentLabel.textLayout;

    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];

 

posted on 2016-10-09 15:55  活最好的自己  阅读(2531)  评论(0编辑  收藏  举报

导航