1
/**
* 专门用来更新标签按钮的frame
*/
- (void)updateTagButtonFrame
{
// 更新标签按钮的frame 3
for (int i = 0; i<self.tagButtons.count; i++) {
XMGTagButton *tagButton = self.tagButtons[i];
if (i == 0) { // 最前面的标签按钮
tagButton.x = 0;
tagButton.y = 0;
} else { // 其他标签按钮
XMGTagButton *lastTagButton = self.tagButtons[i - 1];
// 计算当前行左边的宽度
CGFloat leftWidth = CGRectGetMaxX(lastTagButton.frame) + XMGTagMargin;
// 计算当前行右边的宽度
CGFloat rightWidth = self.contentView.width - leftWidth;
if (rightWidth >= tagButton.width) { // 按钮显示在当前行
tagButton.y = lastTagButton.y;
tagButton.x = leftWidth;
} else { // 按钮显示在下一行
tagButton.x = 0;
tagButton.y = CGRectGetMaxY(lastTagButton.frame) + XMGTagMargin;
}
}
}
}
2
{
//医生领域
CGFloat padding = 5;
CGFloat titBtnX = 15;
CGFloat titBtnY = 5;
CGFloat titBtnH = 25;
CGFloat kwidth = (kScreenWidth-30);
NSString *tags = dominStr;
if (tags!= nil||![tags isEqualToString:@""]) {
NSArray *tagArray = [tags componentsSeparatedByString:@","];
for (int i = 0 ; i<tagArray.count; i++) {
NSString *tagStr = tagArray[i];
if (tagStr.length == 0 || [tagStr isEqualToString:@""] || tagStr == nil) continue;
UILabel *tagBtn = [JMFoundation CommonLabelWithFont:12 textColor:UIColorFromRGB(0x3e2723)];
tagBtn.layer.borderColor = UIColorFromRGB(0x3e2723).CGColor;
tagBtn.layer.borderWidth = 1.5;
tagBtn.text = tagArray[i];
tagBtn.textAlignment = NSTextAlignmentCenter;
tagBtn.layer.cornerRadius = 25/2;
tagBtn.layer.masksToBounds = YES;
//计算文字大小
CGSize titleSize = [tagArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, titBtnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
CGFloat titBtnW = titleSize.width + 2 * padding;
//判断按钮是否超过屏幕的宽
if ((titBtnX + titBtnW) > kwidth) {
titBtnX = 15;
titBtnY += titBtnH + padding;
_dominTagH = titBtnY;
}
//设置按钮的位置
tagBtn.frame = CGRectMake(titBtnX, titBtnY, titBtnW, titBtnH);
titBtnX += titBtnW + padding;
[self.dominMidView addSubview:tagBtn];
}
}
}
网友评论