iOS高级程序师技术群:622177838,期待你的加入
需求:创建多行小标签,标签内容不定
效果:1行4个,最多显示两行
延伸:i %4==3 通过判断换行,重置x轴起始坐标,完美解决~ 可以适用于多行(大于两行)显示
CGFloat Start_X = cell.sigleTitle.frame.origin.x;// 第一个按钮的X坐标
CGFloat Start_Y = cell.sigleTitleSub.frame.origin.y+ cell.sigleTitleSub.frame.size.height;//第一个按钮的Y坐标
CGFloat Width_Space =10.0f;// 2个按钮之间的横间距
CGFloat Height_Space =10.0f;// 竖间距
CGFloat Button_Height =15;//高
NSArray* titleArray =@[@"在售",@"普通住宅",@"花园洋房",@"板楼",@"二手房",@"拆迁房"];
for(int i =0; i < titleArray.count; i++) {
NSInteger page = i /4;
NSString* numTitle = titleArray[i];
CGFloat Button_Width = numTitle.length*15;//宽
UILabel* item = [[UILabelalloc]initWithFrame:CGRectMake(Start_X, page * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height)];
item.font= [UIFontsystemFontOfSize:11];
item.backgroundColor = [UIColor lightGrayColor];
item.textAlignment = NSTextAlignmentCenter;
item.textColor= [UIColorblackColor];
item.text= titleArray[i];
[cell.sigleImageView addSubview:item];
if(i %4==3) {
Start_X = cell.sigleTitle.frame.origin.x;
}else{
Start_X += Button_Width + Width_Space;
}
}
网友评论