自定义标签
NSArray *array = @[@"法人款纠纷",@"水调歌头如何",@"惹人问题个人提取",@"we分为其他国企",@"非诚勿扰",@"呵呵dc凡人歌呵",@"唉",@"饭认为",@"呵呵哒",@"快看看",@"人而且台湾",@"任务分工呆大事件",@"非诚勿fdv扰",@"呵呵呵",@"天热团日活动",@"破开放港口",@"放热她阿尔法人",@"地区の热瓦F人发都很贵",@"非特认为有勿扰",@"呵呵投入和人歌呵",@"耳她人管",@"5特意和雪",@"呵呵哒",@"快看看",@"窿窿啦啦",@"43他",@"合欢大哥花",@"暴ewf走大事件",@"得我同因为我",@"软接头佛陈歌呵"];
CGFloat buttonX=40 ;//初始值x
CGFloat buttonY=100;// 初始值y
CGFloat buttonHeight=35; //固定高度
for ( int i=0; i<array.count ; i++)
{
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:array[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
button.backgroundColor=[UIColor greenColor];
button.titleLabel.font=[UIFont systemFontOfSize:15];
CGFloat ButtonWidth=[array[i] targetSizeForTextWithpurposeSize:CGSizeMake(MAXFLOAT, buttonHeight) WithTextFont:[UIFont systemFontOfSize:15]].width+10;//计算出每一个字符串的宽度
button.frame=CGRectMake(buttonX, buttonY, ButtonWidth, buttonHeight);
if (buttonX+ButtonWidth>YHScreenWith)//当最后一个值大于屏幕时切换下一行
{
buttonX=40;
buttonY = buttonY+buttonHeight+10;
button.frame=CGRectMake(buttonX, buttonY, ButtonWidth, buttonHeight);
}
buttonX =buttonX+ButtonWidth+20;
[self.view addSubview:button];
}
如图所示