OfficialMessageModel.m
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// OfficialMessageModel.m
// DreamSleep
//
// Created by peter on 2022/10/14.
//
// 官方动态内容显示最大行数
#define kMaxOfficialContentLine 5
#import "OfficialMessageModel.h"
@implementation OfficialMessageModel
#pragma mark - YYModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
return @{@"message_id" : @"id"
};
}
//- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
// // YYModel给自定义字段设置默认值
// // self.isOfficial = YES;
// return YES;
//}
#pragma mark - public
+ (UIFont *)contentFont {
return SysFont(14);
}
+ (CGFloat)contentMargin {
return 15;
}
- (NSAttributedString *)contentAttriStr {
NSString *contentStr = self.content ?: @"";
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:[self contentAttributes:NO]];
if (self.isShowAllContent) {
return attStr.copy;
}
if ([self contentMaxLines].count > kMaxOfficialContentLine) {
// 1、获取显示最多行字符串
NSMutableString *mLineStr = [NSMutableString string];
for (NSInteger i = 0; i < kMaxOfficialContentLine; i++) {
[mLineStr appendString:[self contentMaxLines][i]];
}
// 2、截取字符串最后1个位置,用于填充自定义富文本
NSString * subLineStr = [mLineStr substringWithRange:NSMakeRange(0, mLineStr.length - 1)];
// 3、将subLineStr转换未富文本
attStr = [[NSMutableAttributedString alloc] initWithString:subLineStr attributes:[self contentAttributes:NO]];
// 4、自定义结尾富文本
NSMutableAttributedString *dotAtrStr = [[NSMutableAttributedString alloc] initWithString:@"..." attributes:[self contentAttributes:YES]];
[attStr appendAttributedString:dotAtrStr.copy];
}
return attStr.copy;
}
- (CGFloat)imgHeight {
if (self.imgs) {
if (iPhone5) {
return 100;
} else {
NSArray *imgUrls = [self getImgUrlsArr:self.imgs];
if (imgUrls.count == 1 || imgUrls.count == 2) {
return 150;
} else if (imgUrls.count == 3) {
return 95;
}
return 0;
}
}
return 0;
}
- (CGFloat)contentHeight {
CGFloat contentW = [OfficialMessageModel contentMaxW];
return [UILabel getHeightByWidth:contentW attributedText:[self contentAttriStr] font:[OfficialMessageModel contentFont]];
}
- (CGFloat)cellHeight {
NSArray *imgUrls = [self getImgUrlsArr:self.imgs];
CGFloat topH = 67;
CGFloat bottomH = 15;
CGFloat contentH = [self contentHeight];
CGFloat imgTextSpace = imgUrls.count ? 12 : 0;
CGFloat imgH = [self imgHeight];
return topH + bottomH + contentH + imgTextSpace + imgH;
}
- (NSArray *)getImgUrlsArr:(NSString *)imgUrls {
if (imgUrls && imgUrls.length) {
return [imgUrls componentsSeparatedByString:@"|"];
}
return @[];
}
#pragma mark - private
+ (CGFloat)contentMaxW {
return kScreenWidth - 4 * [OfficialMessageModel contentMargin];
}
- (NSArray *)contentMaxLines {
return [self.content getLinesArrayOfStringWidth:[OfficialMessageModel contentMaxW] attributes:@{NSFontAttributeName : [OfficialMessageModel contentFont]}];
}
- (NSDictionary *)contentAttributes:(BOOL)isTail {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = [OfficialMessageModel contentFont].lineHeight;
paragraphStyle.maximumLineHeight = [OfficialMessageModel contentFont].lineHeight;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSMutableDictionary *mAttributes = [NSMutableDictionary dictionaryWithDictionary:
@{NSFontAttributeName : [OfficialMessageModel contentFont],
NSParagraphStyleAttributeName : paragraphStyle}];
if (isTail) {
[mAttributes setObject:BrandColor forKey:NSForegroundColorAttributeName];
}
return mAttributes.copy;
}
@end