ComReplyModel.m
4.5 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
124
125
126
//
// ComReplyModel.m
// DreamSleep
//
// Created by peter on 2022/10/8.
//
#import "ComReplyModel.h"
// 回复内容显示最大行数
#define kMaxReplyLine 2
@implementation ComReplyModel
#pragma mark - 模型字段映射
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
return @{@"commentModel" : @"comment",
@"replyModel" : @"reply"
};
}
#pragma mark - public
- (NSString *)replyNickName {
return [NSString stringWithFormat:@"%@:", self.replyModel.nick_name];
}
+ (UIFont *)commentContentFont {
return SysFont(14);
}
+ (UIFont *)replyNickNameFont {
return SysFont(15);
}
+ (UIFont *)replyContentFont {
return SysFont(14);
}
- (NSAttributedString *)replyContentAttriStr {
NSString *replyContentStr = self.replyModel.content ? self.replyModel.content : @"";
NSMutableAttributedString *replyAttStr = [[NSMutableAttributedString alloc] initWithString:replyContentStr attributes:[self replyContentAttributes:NO]];
if ([self replyContentMaxLines].count > kMaxReplyLine) {
// 1、获取显示最多行字符串
NSMutableString *mLineStr = [NSMutableString string];
for (NSInteger i = 0; i < kMaxReplyLine; i++) {
[mLineStr appendString:[self replyContentMaxLines][i]];
}
// 2、截取字符串最后1个位置,用于填充自定义富文本
NSString * subLineStr = [mLineStr substringWithRange:NSMakeRange(0, mLineStr.length - 1)];
// 3、将subLineStr转换未富文本
replyAttStr = [[NSMutableAttributedString alloc] initWithString:subLineStr attributes:[self replyContentAttributes:NO]];
// 4、自定义结尾富文本
NSMutableAttributedString *dotAtrStr = [[NSMutableAttributedString alloc] initWithString:@"..." attributes:[self replyContentAttributes:YES]];
[replyAttStr appendAttributedString:dotAtrStr.copy];
}
return replyAttStr.copy;
}
- (CGFloat)commentContentLeftMargin {
return 84;
}
- (CGFloat)commentContentRightMargin {
return 30;
}
- (CGFloat)commentContentHeight {
CGFloat commentContentW = kScreenWidth - [self commentContentLeftMargin] - [self commentContentRightMargin];
NSString *content = self.commentModel.content;
return [UILabel getHeightByWidth:commentContentW text:content font:[ComReplyModel commentContentFont]];
}
- (CGFloat)replyNickNameW {
CGFloat replyNameLabW = [UILabel getWidthWithText:[self replyNickName] font:[ComReplyModel replyNickNameFont]];
return replyNameLabW > 75 ? 75 : replyNameLabW;
}
- (CGSize)replyContentSize {
CGFloat replyContentW = [self replyContentMaxW];
CGFloat replyContentH = [UILabel getHeightByWidth:replyContentW attributedText:[self replyContentAttriStr] font:[ComReplyModel replyContentFont]];
return CGSizeMake(replyContentW, replyContentH);
}
- (CGFloat)replyViewHeight {
CGFloat replayViewH = 0;
if (self.replyModel) {
replayViewH = 9 + [self replyContentSize].height + (self.commentModel.total_replys > 1 ? 37 : 9);
}
return replayViewH;
}
- (CGFloat)commentCellHeight {
CGFloat topH = 48;
CGFloat commentH = [self commentContentHeight];
CGFloat replayViewH = [self replyViewHeight] + (self.replyModel ? 12 : 0);
CGFloat bottomMargin = 15;
return topH + commentH + replayViewH + bottomMargin;
}
#pragma mark - private
- (NSArray *)replyContentMaxLines {
return [self.replyModel.content getLinesArrayOfStringWidth:[self replyContentMaxW] attributes:@{NSFontAttributeName : [ComReplyModel replyContentFont]}];
}
- (CGFloat)replyContentMaxW {
return kScreenWidth - [self commentContentLeftMargin] - [self commentContentRightMargin] - [self replyNickNameW] - 16;
}
- (NSDictionary *)replyContentAttributes:(BOOL)isTail {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = [ComReplyModel replyContentFont].lineHeight;
paragraphStyle.maximumLineHeight = [ComReplyModel replyContentFont].lineHeight;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSMutableDictionary *mAttributes = [NSMutableDictionary dictionaryWithDictionary:
@{NSFontAttributeName : [ComReplyModel replyContentFont],
NSParagraphStyleAttributeName : paragraphStyle}];
// 只有1条回复高亮...
if (isTail && self.commentModel.total_replys < 2) {
[mAttributes setObject:BrandColor forKey:NSForegroundColorAttributeName];
}
return mAttributes.copy;
}
@end