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
1.在使用 YYLabel 時,必定要給該對象設置背景顏色,否則沒法顯示.app
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 視圖是否存在:若不存在,就建立.atom
3.YYLabel 視圖的 attributedText屬性,和普通 UILabel 的 attributedText 屬性,基本相同,賦值方法均爲_chatMsgLabel.attributedText = attrStr;並且均可以直接使用 NSAttributedString 對象的 appendAttributedString 屬性/ addAttribute 屬性.url
(PS:使用yy_attachmentStringWithContent:...時,就不要使用 addAttribute 屬性了,)spa
YYLabel 視圖一個很大的便捷是:使用 rest
NSMutableAttributedString 對象的拓展 yy_color 和 yy_setColor:range: 屬性,快速設置文字的屬性,代替了複雜的addAttribute 屬性.code
惟一不一樣的一點就是:YYLabel沒法識別 NSAttributedString 類的 attributedStringWithAttachment 方法添加的圖片.YYLabel 添加圖片須要 NSAttributedString的拓展類方法:yy_attachmentStringWithContent:....
orm
4.在cell 中調用YYLabel時,能夠用其對象的 textLayout 屬性來代替 attributedText 給YYLabel賦值,同時使用 textLayout 屬性的 textBoundingSize 屬性對象
來設置 YYLabel 的寬高.代碼以下:blog
(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];