UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 300)];

view.backgroundColor=[UIColor grayColor];

[self.view addSubview:view];

NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:@"园园",@"园园",@"蛋蛋园园",@"啊啊啊啊",@"拉",@"呀呀呀啊呀啊呀呀", nil];

int i=0;

CGFloat btnH=30;

CGFloat yOffset=10;

CGFloat xOffset=15;

for (NSString *string in array) {

CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, btnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17] } context:nil];

CGFloat w = rect.size.width+10;

NSLog(@"%f",yOffset+w);

if (xOffset+w>self.view.frame.size.width-20) {

xOffset=10;

yOffset+=(btnH+15);

}

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

button.titleLabel.font=[UIFont systemFontOfSize:17];

[button setTitle:string forState:(UIControlStateNormal)];

[button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

//自己 加  Button的点击 事件

[button setBackgroundColor:[UIColor whiteColor]];

button.layer.cornerRadius=5;

button.clipsToBounds=YES;

button.tag=1000+i;

button.frame = CGRectMake(xOffset, yOffset, w, btnH);

[view addSubview:button];

xOffset+=(w+10);

++i;

}

05-11 15:10