- (void)createBtn {
CGFloat spacing = 10;
CGFloat x = 0;
CGFloat y = 10;
CGFloat btnH = 40;
for (int i = 0; i < _arr.count; i ++) {
UIButton *btn = [UIButton buttonWithType:0];
[btn setTitle:[_arr objectAtIndex:i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor lightGrayColor];
btn.titleLabel.font = [UIFont systemFontOfSize:17];
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
CGSize titleSize = [[self.arr objectAtIndex:i] boundingRectWithSize:CGSizeMake(MAXFLOAT, btnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:btn.titleLabel.font} context:nil].size;
CGFloat btnW = titleSize.width + 2 * spacing;
if (x + btnW > SCREEN_WIDTH) {
x = 0;
y += btnH + spacing;
}
btn.frame = CGRectMake(x, y, btnW, btnH);
x += btnW + spacing;
}
}
网友评论