forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTopicCell.m
More file actions
executable file
·252 lines (218 loc) · 10.3 KB
/
ProjectTopicCell.m
File metadata and controls
executable file
·252 lines (218 loc) · 10.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//
// ProjectTopicCell.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-20.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kProjectTopicCell_PadingLeft 55.0
#define kProjectTopicCell_PadingRight 30.0
#define kProjectTopicCell_ContentWidth (kScreen_Width - kProjectTopicCell_PadingLeft - kProjectTopicCell_PadingRight)
#define kProjectTopicCell_ContentHeightMax 40.0
#define kProjectTopicCell_ContentFont [UIFont systemFontOfSize:16]
#define kProjectTopicCellTagsView_Font [UIFont systemFontOfSize:12]
#import "ProjectTopicCell.h"
#import "ProjectTag.h"
#import "ProjectTagLabel.h"
@interface ProjectTopicCell ()
@property (strong, nonatomic) UILabel *titleLabel, *userNameLabel, *timeLabel, *commentCountLabel, *numLabel;
@property (strong, nonatomic) UIImageView *userIconView, *timeClockIconView, *commentIconView;
@property (strong, nonatomic) ProjectTopicCellTagsView *tagsView;
@end
@implementation ProjectTopicCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
self.backgroundColor = [UIColor clearColor];
if (!_userIconView) {
_userIconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 33, 33)];
[_userIconView doCircleFrame];
[self.contentView addSubview:_userIconView];
}
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kProjectTopicCell_PadingLeft, 12, kProjectTopicCell_ContentWidth, 20)];
_titleLabel.font = kProjectTopicCell_ContentFont;
_titleLabel.textColor = [UIColor colorWithHexString:@"0x222222"];
[self.contentView addSubview:_titleLabel];
}
if (!_tagsView) {
_tagsView = [ProjectTopicCellTagsView viewWithTags:nil];
[self.contentView addSubview:_tagsView];
}
if (!_numLabel) {
_numLabel = [[UILabel alloc] initWithFrame:CGRectMake(kProjectTopicCell_PadingLeft, 0, 150, 15)];
_numLabel.backgroundColor = [UIColor clearColor];
_numLabel.font = [UIFont systemFontOfSize:10];
_numLabel.textColor = [UIColor colorWithHexString:@"0x222222"];
[self.contentView addSubview:_numLabel];
}
if (!_userNameLabel) {
_userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kProjectTopicCell_PadingLeft, 0, 150, 15)];
_userNameLabel.backgroundColor = [UIColor clearColor];
_userNameLabel.font = [UIFont systemFontOfSize:10];
_userNameLabel.textColor = [UIColor colorWithHexString:@"0x666666"];
[self.contentView addSubview:_userNameLabel];
}
if (!_timeClockIconView) {
_timeClockIconView = [[UIImageView alloc] initWithFrame:CGRectMake(kProjectTopicCell_PadingLeft, 0, 12, 12)];
_timeClockIconView.image = [UIImage imageNamed:@"time_clock_icon"];
[self.contentView addSubview:_timeClockIconView];
}
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kProjectTopicCell_PadingLeft, 0, 80, 15)];
_timeLabel.font = [UIFont systemFontOfSize:10];
_timeLabel.textColor = [UIColor colorWithHexString:@"0x999999"];
[self.contentView addSubview:_timeLabel];
}
if (!_commentIconView) {
_commentIconView = [[UIImageView alloc] initWithFrame:CGRectMake(kProjectTopicCell_PadingLeft, 0, 12, 12)];
[_commentIconView setImage:[UIImage imageNamed:@"topic_comment_icon"]];
[self.contentView addSubview:_commentIconView];
}
if (!_commentCountLabel) {
_commentCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(kProjectTopicCell_PadingLeft, 0, 20, 15)];
_commentCountLabel.font = [UIFont systemFontOfSize:10];
_commentCountLabel.minimumScaleFactor = 0.5;
_commentCountLabel.adjustsFontSizeToFitWidth = YES;
_commentCountLabel.textColor = [UIColor colorWithHexString:@"0x999999"];
[self.contentView addSubview:_commentCountLabel];
}
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
if (!_curTopic) {
return;
}
[_userIconView sd_setImageWithURL:[_curTopic.owner.avatar urlImageWithCodePathResizeToView:_userIconView] placeholderImage:kPlaceholderMonkeyRoundView(_userIconView)];
[_titleLabel setLongString:_curTopic.title withFitWidth:kProjectTopicCell_ContentWidth maxHeight:kProjectTopicCell_ContentHeightMax];
CGFloat curBottomY = 12 + [_curTopic.title getHeightWithFont:kProjectTopicCell_ContentFont constrainedToSize:CGSizeMake(kProjectTopicCell_ContentWidth, kProjectTopicCell_ContentHeightMax)] + 12;
CGFloat curRightX = kProjectTopicCell_PadingLeft;
_tagsView.tags = _curTopic.labels;
[_tagsView setOrigin:CGPointMake(curRightX, curBottomY)];
curBottomY += [ProjectTopicCellTagsView getHeightForTags:_curTopic.labels];
[_numLabel setOrigin:CGPointMake(curRightX, curBottomY)];
_numLabel.text = [NSString stringWithFormat:@"#%@", _curTopic.number.stringValue];
[_numLabel sizeToFit];
curRightX = _numLabel.maxXOfFrame + 10;
[_userNameLabel setOrigin:CGPointMake(curRightX, curBottomY)];
_userNameLabel.text = _curTopic.owner.name;
[_userNameLabel sizeToFit];
curRightX = _userNameLabel.maxXOfFrame+ 10;
[_timeClockIconView setOrigin:CGPointMake(curRightX, curBottomY)];
[_timeLabel setOrigin:CGPointMake(curRightX + 15, curBottomY)];
_timeLabel.text = [_curTopic.created_at stringDisplay_MMdd];
[_timeLabel sizeToFit];
curRightX = _timeLabel.maxXOfFrame + 10;
[_commentIconView setOrigin:CGPointMake(curRightX, curBottomY)];
[_commentCountLabel setOrigin:CGPointMake(curRightX +15, curBottomY)];
_commentCountLabel.text = _curTopic.child_count.stringValue;
[_commentCountLabel sizeToFit];
}
+(CGFloat)cellHeightWithObj:(id)aObj{
CGFloat cellHeight = 0;
if ([aObj isKindOfClass:[ProjectTopic class]]) {
ProjectTopic *curTopic = (ProjectTopic *)aObj;
cellHeight += 12 + [curTopic.title getHeightWithFont:kProjectTopicCell_ContentFont constrainedToSize:CGSizeMake(kProjectTopicCell_ContentWidth, kProjectTopicCell_ContentHeightMax)] + 12;
cellHeight += [ProjectTopicCellTagsView getHeightForTags:curTopic.labels];
cellHeight += 15 + 12;
}
return cellHeight;
}
@end
@interface ProjectTopicCellTagsView ()
@property (strong, nonatomic) NSMutableArray *tagLabelList;
@end
@implementation ProjectTopicCellTagsView
static CGFloat kProjectTopicCellTagsView_Height_PerLine = 25.0;
static CGFloat kProjectTopicCellTagsView_Padding_Space = 5.0;
static CGFloat kProjectTopicCellTagsView_Padding_Content = 10.0;
- (instancetype)initWithTags:(NSArray *)tags{
self = [super init];
if (self) {
_tagLabelList = [NSMutableArray new];
self.tags = tags;
}
return self;
}
+ (instancetype)viewWithTags:(NSArray *)tags{
ProjectTopicCellTagsView *tagsView = [[self alloc] initWithTags:tags];
return tagsView;
}
+ (CGFloat)getHeightForTags:(NSArray *)tags{
CGFloat height = 0;
if (tags.count > 0) {
CGFloat viewWidth = kProjectTopicCell_ContentWidth;
CGFloat curX = 0, curY = 0;
for (ProjectTag *curTag in tags) {
CGFloat textWidth = [curTag.name getWidthWithFont:kProjectTopicCellTagsView_Font constrainedToSize:CGSizeMake(CGFLOAT_MAX, kProjectTopicCellTagsView_Height_PerLine)];
CGFloat curTagWidth = MIN(textWidth + kProjectTopicCellTagsView_Padding_Content, viewWidth);
curX += curTagWidth;
if (curX > viewWidth) {
curY += kProjectTopicCellTagsView_Height_PerLine;
curX = curTagWidth + kProjectTopicCellTagsView_Padding_Space;
}else{
curX += kProjectTopicCellTagsView_Padding_Space;
}
}
height = curY + kProjectTopicCellTagsView_Height_PerLine;
height += 7.0;//与下面内容的间隔
}else{
height = 0.0;
}
return height;
}
#define kProjectTopicCellTagsView_HeightPerLine 25.0
- (void)setTags:(NSArray *)tags{
_tags = tags;
if (_tags.count > 0) {
if (!_tagLabelList) {
_tagLabelList = [NSMutableArray new];
}
CGPoint curPoint = CGPointZero;
CGFloat viewWidth = kProjectTopicCell_ContentWidth;
int index;
for (index = 0; index < _tags.count; index++) {
ProjectTagLabel *curLabel;
if (_tagLabelList.count > index) {
curLabel = _tagLabelList[index];
curLabel.curTag = _tags[index];
}else{
curLabel = [ProjectTagLabel labelWithTag:_tags[index] font:kProjectTopicCellTagsView_Font height:20 widthPadding:kProjectTopicCellTagsView_Padding_Content];
[_tagLabelList addObject:curLabel];
}
CGFloat curPointRightX = curPoint.x + MIN(CGRectGetWidth(curLabel.frame), viewWidth);
if (curPointRightX > viewWidth) {
curPoint.x = 0;
curPoint.y += kProjectTopicCellTagsView_Height_PerLine;
}
[curLabel setOrigin:curPoint];
[self addSubview:curLabel];
//下一个点
curPoint.x += CGRectGetWidth(curLabel.frame) +kProjectTopicCellTagsView_Padding_Space;
}
if (_tagLabelList.count > index) {
[_tagLabelList enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UILabel *obj, NSUInteger idx, BOOL *stop) {
if (idx >= index) {
[obj removeFromSuperview];
}else{
*stop = YES;
}
}];
[_tagLabelList removeObjectsInRange:NSMakeRange(index, _tagLabelList.count -index)];
}
[self setSize:CGSizeMake(kScreen_Width, curPoint.y + kProjectTopicCellTagsView_Height_PerLine)];
self.hidden = NO;
}else{
[self.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperview];
}];
[_tagLabelList removeAllObjects];
self.hidden = YES;
}
}
@end