通常项目中会遇到一些特殊标签,并且这个标签个数及内容并不确定,甚至点击效果也不同.
思路如下,这里是展现一行4个标签,主要代码如下:
1. 创建所需标签背景及标签数组
// 服务标签视图
@property (nonatomic, strong) UIView *tagsView;
// 服务模型数组
@property (nonatomic, strong) NSMutableArray *serveArray;
- (UIView *)tagsView {
if (!_tagsView) {
UIView *tagsView = [[UIView alloc] init];
tagsView.frame = CGRectMake(0, 0, kContentViewWidth, 70);
tagsView.backgroundColor = [UIColor whiteColor];
_tagsView = tagsView;
}
return _tagsView;
}
- (NSMutableArray *)serveArray {
if (!_serveArray) {
NSMutableArray *array = [NSMutableArray array];
_serveArray = array;
}
return _serveArray;
}
2. 根据网络请求获取标签总数
// 标签总数
// NSInteger count = [json[@"data"] count];
self.serveArray = [YYPGoodsModel objectArrayWithKeyValuesArray:json[@"data"]];
3. 根据标签总数计算标签背景总高度
CGFloat marginX = 15;
NSInteger maxCol = 4;
CGFloat btnH = 40;
// CGFloat btnW = (kContentViewWidth - marginX * (maxCol + 1)) / maxCol;
CGFloat top = 15;
CGFloat tagViewH = (btnH + marginX) * (self.serveArray.count / maxCol + 1) + top; // 标签背景高度
// label
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor whiteColor];
label.frame = CGRectMake(0, CGRectGetMaxY(searchBar.frame) + 10, kContentViewWidth, 50);
label.text = @" 服务";
label.textColor = YYPColor(102, 102, 102);
label.font = [UIFont systemFontOfSize:18];
[self.CollectHeaderView addSubview:label];
// line
UIView *line = [[UIView alloc] init];
line.backgroundColor = YYPColor(204, 204, 204);
line.frame = CGRectMake(0, CGRectGetMaxY(label.frame) - 1, kContentViewWidth, 1);
[self.CollectHeaderView addSubview:line];
// 标签背景
self.tagsView.frame = CGRectMake(0, CGRectGetMaxY(line.frame), kContentViewWidth, tagViewH);
[self.CollectHeaderView addSubview:self.tagsView];
// 分割线
UIView *line2 = [[UIView alloc] init];
line2.backgroundColor = YYPColor(243, 243, 243);
line2.frame = CGRectMake(0, tagViewH - 1, kContentViewWidth, 1);
[self.tagsView addSubview:line2];
4. 循环创建标签并赋值标签title内容名字
self.serveArray = [YYPGoodsModel objectArrayWithKeyValuesArray:json[@"data"]];
CGFloat top = 15;
CGFloat marginX = 15;
NSInteger maxCol = 4;
CGFloat btnH = 40;
CGFloat btnW = (kContentViewWidth - marginX * (maxCol + 1)) / maxCol;
// 标签名字
for (NSInteger i = 0; i < self.serveArray.count; i++) {
NSString *name = [self.serveArray[i] NAME];
// NSString *name = json[@"data"][i][@"NAME"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = YYPColor(238, 238, 238);
btn.layer.cornerRadius = YYP_btnCornerRadius;
btn.clipsToBounds = YES;
btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[btn setTitleColor:YYPColor(102, 102, 102) forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
// 这里点击效果根据需求自定义
[btn addTarget:self action:@selector(chooseMark:) forControlEvents:UIControlEventTouchUpInside];
NSInteger col = i % maxCol; //列
btn.x = marginX + col * (btnW + marginX);
NSInteger row = i / maxCol; //行
btn.y = top + row * (btnH + marginX);
btn.width = btnW;
btn.height = btnH;
[btn setTitle:name forState:UIControlStateNormal];
[self.tagsView addSubview:btn];
}
PS:针对获取个数时会是整数时,则行数会多一行问题进行修复,若 列个数取余为整数则不需要加1.
NSInteger rowCount = (self.serveArray.count % maxCol == 0) ? self.serveArray.count / maxCol : (self.serveArray.count / maxCol + 1); // 行数
CGFloat tagViewH = (btnH + marginX) * rowCount + top; // 标签背景高度