ArticleCell.m
5.3 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// ArticleCell.m
// DreamSleep
//
// Created by peter on 2022/10/19.
//
#import "ArticleCell.h"
@interface ArticleCell ()
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) UIImageView *coverIV;
@property (nonatomic, strong) UIView *tagsView;
@property (nonatomic, strong) UILabel *readCountLab;
@end
@implementation ArticleCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self cornerRadius:12];
self.dk_backgroundColorPicker = DKColorPickerWithKey(CornerViewBG);
[self.contentView addSubview:self.titleLab];
[self.contentView addSubview:self.contentLab];
[self.contentView addSubview:self.coverIV];
[self.contentView addSubview:self.tagsView];
[self.contentView addSubview:self.readCountLab];
[self.coverIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.contentView).offset(-8);
make.centerY.equalTo(self.contentView);
make.size.mas_equalTo(CGSizeMake(86, 86));
}];
[self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(8);
make.top.equalTo(self.contentView).offset(12);
make.right.equalTo(self.coverIV.mas_left).offset(-12);
}];
[self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.titleLab);
make.top.equalTo(self.contentView).offset(43);
}];
[self.tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.titleLab);
make.height.equalTo(@21);
make.bottom.equalTo(self.contentView).offset(-12);
}];
[self.readCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.coverIV);
make.height.equalTo(@25);
}];
[self layoutIfNeeded];
[self.readCountLab setCornerRadiusRect:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadius:12];
}
return self;
}
- (void)setModel:(ArticleModel *)model {
_model = model;
self.titleLab.text = model.title;
self.contentLab.text = model.content;
[self.coverIV yy_setImageWithURL:[NSURL URLWithString:model.cover] placeholder:[UIImage defaultPlaceholderWithSize:CGSizeMake(86, 86)]];
self.readCountLab.text = [NSString stringWithFormat:@"%ld人阅读过", model.read_count];
// tag标签最多只有3个
NSArray *tagsTitleArr = model.tags ? [model.tags componentsSeparatedByString:@"|"] : @[];
NSArray *tagTitleColors = @[BrandColor];
NSArray *tagBgColors = @[ColorFromHexA(0x62C3D5, .2)];
if (tagsTitleArr.count == 2) {
tagTitleColors = @[BrandColor, ColorFromHex(0x9377FC)];
tagBgColors = @[ColorFromHexA(0x62C3D5, .2), ColorFromHexA(0x9377FC, .2)];
} else if (tagsTitleArr.count == 3) {
tagTitleColors = @[BrandColor, ColorFromHex(0x9377FC), ColorFromHex(0x5E9EFC)];
tagBgColors = @[ColorFromHexA(0x62C3D5, .2), ColorFromHexA(0x9377FC, .2), ColorFromHexA(0x5E9EFC, .2)];
}
[self.tagsView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperview];
}];
CGFloat totalW = 0;
for (int idx = 0; idx < tagsTitleArr.count; idx++) {
NSString *tagTitle = [NSString stringWithFormat:@"#%@", tagsTitleArr[idx]];
CGFloat tagTitleW = [UILabel getWidthWithText:tagTitle font:SysFont(12)] + 16;
UILabel *tagLab = [UILabel labWithTextColor:tagTitleColors[idx] font:SysFont(12)];
tagLab.text = tagTitle;
tagLab.textAlignment = NSTextAlignmentCenter;
tagLab.backgroundColor = tagBgColors[idx];
[tagLab cornerRadius:10.5];
tagLab.frame = CGRectMake(totalW + idx * 8, 0, tagTitleW, 21);
totalW += tagTitleW;
[self.tagsView addSubview:tagLab];
}
return;
}
#pragma mark - lazy
- (UILabel *)titleLab {
if (!_titleLab) {
_titleLab = [UILabel labWithFont:BoldFont(15)];
_titleLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
}
return _titleLab;
}
- (UILabel *)contentLab {
if (!_contentLab) {
_contentLab = [UILabel labWithFont:SysFont(14)];
_contentLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
}
return _contentLab;
}
- (UIImageView *)coverIV {
if (!_coverIV) {
_coverIV = [UIImageView new];
[_coverIV cornerRadius:12];
_coverIV.dk_alphaPicker = DKAlphaPickerWithAlphas(1.f, 0.5f, 0.1f);
}
return _coverIV;
}
- (UIView *)tagsView {
if (!_tagsView) {
_tagsView = [UIView new];
_tagsView.clipsToBounds = YES;
}
return _tagsView;
}
- (UILabel *)readCountLab {
if (!_readCountLab) {
_readCountLab = [UILabel labWithFont:SysFont(11)];
_readCountLab.textAlignment = NSTextAlignmentCenter;
_readCountLab.textColor = ColorFromHexA(0xFFFFFF, .7);
_readCountLab.backgroundColor = ColorFromHexA(0x161E38, .35);
}
return _readCountLab;
}
@end