控件宽度,个数都不确定的情况下, 实现折行和居中效果. 这里以UILabel 和 UIButton为例 , 其他控件思路一致 .
左对齐版:
先看下效果图:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgHeight;
@property (nonatomic, strong) NSArray *scoreArrs;
@property (nonatomic, assign) int lineNum;
@property (nonatomic, strong) UIButton *lastbtn;
@property (nonatomic, strong) NSMutableArray *btnArr;
-(NSArray *)scoreArrs{
if (_scoreArrs == nil) {
_scoreArrs = @[@"112",@"这个放不下",@"112",@"112",@"这个放不下",@"112"];
}
return _scoreArrs;
}
-(NSMutableArray *)btnArr{
if (_btnArr == nil) {
_btnArr = [[NSMutableArray alloc] init];
}
return _btnArr;
}
具体实现:
//设置热搜按钮
-(void)setupBtnView{
self.lineNum = 0;
CGFloat margin = 10;
CGFloat between = 11;
for (int i = 0; i < self.scoreArrs.count; i++) {
UIButton *btn = [[UIButton alloc] init];
[btn setTitle:self.scoreArrs[i] forState:UIControlStateNormal];
[btn setTitle:self.scoreArrs[i] forState:UIControlStateHighlighted];
btn.tag = i;
UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
btn.titleLabel.font = fnt;
btn.layer.cornerRadius = 4;
btn.layer.masksToBounds = YES;
[btn setBackgroundColor:kFormator_COLOR];
[btn addTarget:self action:@selector(clickWithBtn:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitleColor:kGRAY_COLOR forState:UIControlStateNormal];
CGSize size = [btn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
CGFloat nameH = size.height + 14;
CGFloat nameW = size.width + 20;
if (i == 0) {
btn.frame = CGRectMake(margin, self.lineNum * nameH +margin, nameW,nameH);
self.lastbtn = btn;
} else {
CGFloat tempSpace = btnView.frame.size.width - CGRectGetMaxX(self.lastbtn.frame)- margin - between;
if (tempSpace >= nameW) {
btn.frame = CGRectMake(CGRectGetMaxX(self.lastbtn.frame)+ between, self.lineNum * (nameH + between)+margin, nameW,nameH);
} else {
self.lineNum ++;
btn.frame = CGRectMake(margin, self.lineNum * (nameH + between)+margin, nameW,nameH);
}
self.lastbtn = btn;
}
if (i == self.scoreArrs.count -1) {
self.bgHeight.constant = CGRectGetMaxY(btn.frame) + margin;
}
[btnView addSubview:btn];
}
}```
#### 居中版:
先看下效果图:
![居中版效果图.png](http://upload-images.jianshu.io/upload_images/1307566-006f45d6ed051943.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
@property (weak, nonatomic) IBOutlet UIButton *bgview;
@property (nonatomic, strong) NSArray *scoreArrs;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgHeight;
@property (nonatomic, strong) UIButton *lastbtn;
@property (nonatomic, assign) int lineNum;
@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) NSMutableArray *tempArr;
@property (nonatomic, strong) UIButton *tempLastbtn;```
-(NSArray *)scoreArrs{
if (_scoreArrs == nil) {
_scoreArrs = @[@"112",@"112",@"112",@"112",@"这个放不下",@"112",@"112",@"这个放不下",@"这个放不下",@"这个放不下",@"这个放不下",@"这个放不下",@"这个放不下",@"112",@"112",@"这个放不下",@"112",@"112",@"这个放不下",@"这个放不下",@"112",@"112",@"这个放不下",@"这个放不下",@"这个放不下",@"这个放不下",@"这个放不下",@"这个放不下",@"112",@"112",@"这个放不下",@"112",@"112",@"这个放不下",@"112"];
}
return _scoreArrs;
}
-(NSMutableArray *)btnArr{
if (_btnArr == nil) {
_btnArr = [[NSMutableArray alloc] init];
}
return _btnArr;
}
-(NSMutableArray *)tempArr{
if (_tempArr == nil) {
_tempArr = [[NSMutableArray alloc] init];
}
return _tempArr;
}
具体实现:
self.lineNum = 0;
CGFloat margin = 10;
CGFloat between = 8;
for (int i = 0; i<self.scoreArrs.count; i++) {
UIButton *btn = [[UIButton alloc] init];
[btn setTitle:self.scoreArrs[i] forState:UIControlStateNormal];
[btn setTitle:self.scoreArrs[i] forState:UIControlStateHighlighted];
btn.tag = i;
UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
btn.titleLabel.font = fnt;
btn.layer.cornerRadius = 4;
btn.layer.borderWidth = 1;
btn.layer.borderColor = [UIColor colorWithRed:1.000 green:0.153 blue:0.815 alpha:1.000].CGColor;
btn.layer.masksToBounds = YES;
[btn addTarget:self action:@selector(clickWithBtn:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
CGSize size = [btn.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
CGFloat nameH = size.height + 8;
CGFloat nameW = size.width + 8;
if (i == 0) {
btn.frame = CGRectMake(margin, self.lineNum * nameH +margin, nameW,nameH);
self.lastbtn = btn;
[self.btnArr addObject:btn];
} else {
CGFloat tempSpace = self.bgview.frame.size.width - CGRectGetMaxX(self.lastbtn.frame)- margin - between;
if (tempSpace >= nameW) {
btn.frame = CGRectMake(CGRectGetMaxX(self.lastbtn.frame)+ between, self.lineNum * (nameH + between)+margin, nameW,nameH);
[self.btnArr addObject:btn];
} else {
for (int j = 0; j<self.btnArr.count; j++) {
UIButton *tempbtn = self.btnArr[j];
int tempY =(int) tempbtn.frame.origin.y;
int tempbtnY = (int)self.lineNum * (nameH + between)+margin;
if ( tempY == tempbtnY ) {
[self.tempArr addObject:tempbtn];
}
}
for (int m = 0; m<self.tempArr.count; m++) {
UIButton *tempbt = self.tempArr[m];
CGSize size = [tempbt.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
CGFloat tempH = size.height + 8;
CGFloat tempW = size.width + 8;
UIButton *tempLastbtn = self.tempArr[self.tempArr.count -1];
CGFloat mm = CGRectGetMaxX(tempLastbtn.frame) - margin;
CGFloat mn = self.bgview.frame.size.width - mm;
CGFloat tempMar = mn*0.5;
if (m == 0) {
tempbt.frame = CGRectMake(tempMar, self.lineNum * (tempH + between)+margin, tempW,tempH);
self.tempLastbtn = tempbt;
} else {
tempbt.frame = CGRectMake(CGRectGetMaxX(self.tempLastbtn.frame)+ between, self.lineNum * (tempH + between)+margin, tempW,tempH);
self.tempLastbtn = tempbt;
}
}
[self.tempArr removeAllObjects];
self.lineNum ++;
btn.frame = CGRectMake(margin, self.lineNum * (nameH + between)+margin, nameW,nameH);
[self.btnArr addObject:btn];
}
self.lastbtn = btn;
if (i == self.scoreArrs.count -1) {
for (int j = 0; j<self.btnArr.count; j++) {
UIButton *tempbtn = self.btnArr[j];
int tempY =(int) tempbtn.frame.origin.y;
int tempbtnY = (int)self.lineNum * (nameH + between)+margin;
if ( tempY == tempbtnY ) {
[self.tempArr addObject:tempbtn];
}
}
for (int m = 0; m<self.tempArr.count; m++) {
UIButton *tempbt = self.tempArr[m];
CGSize size = [tempbt.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
CGFloat tempH = size.height + 8;
CGFloat tempW = size.width + 8;
UIButton *tempLastbtn = self.tempArr[self.tempArr.count -1];
CGFloat mm = CGRectGetMaxX(tempLastbtn.frame) - margin;
CGFloat mn = self.bgview.frame.size.width - mm;
CGFloat tempMar = mn*0.5;
if (m == 0) {
tempbt.frame = CGRectMake(tempMar, self.lineNum * (tempH + between)+margin, tempW,tempH);
self.tempLastbtn = tempbt;
} else {
tempbt.frame = CGRectMake(CGRectGetMaxX(self.tempLastbtn.frame)+ between, self.lineNum * (tempH + between)+margin, tempW,tempH);
self.tempLastbtn = tempbt;
}
}
[self.tempArr removeAllObjects];
}
}
if (i == self.scoreArrs.count -1) {
self.bgHeight.constant = CGRectGetMaxY(btn.frame) + margin;
}
[self.bgview addSubview:btn];
}
- (void)clickWithBtn:(UIButton *)btn{
btn.selected = !btn.selected;
if (btn.selected) {
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
} else {
[btn setBackgroundColor:[UIColor whiteColor]];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}
注:居中实现方式有点问题, 不应该用Y值来判断, 应该使用计数num来判断, 更准确 .