iOS for循环创建button,button的宽度根据上面的文字来自适应.

NSArray *arr = @[@"无知",@"风云变幻",@"施耐庵",@"唉",@"西门吹雪",@"呵呵哒",@"快看看",@"窿窿啦啦",@"一杆禽兽狙",@"合欢花",@"暴走大事件",@"非诚勿扰",@"呵呵呵"];CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离CGFloat h = 200;//用来控制button距离父视图的高for (int i = 0; i < arr.count; i++) {UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];button.tag = 100 + i;button.backgroundColor = [UIColor greenColor];[button addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//根据计算文字的大小NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};CGFloat length = [arr[i] boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;//为button赋值[button setTitle:arr[i] forState:UIControlStateNormal];//设置button的framebutton.frame = CGRectMake(10 + w, h, length + 15 , 30);//当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度if(10 + w + length + 15 > 320){w = 0; //换行时将w置为0h = h + button.frame.size.height + 10;//距离父视图也变化button.frame = CGRectMake(10 + w, h, length + 15, 30);//重设button的frame}w = button.frame.size.width + button.frame.origin.x;[self.view addSubview:button];}点击事件- (void)handleClick:(UIButton *)btn{NSLog(@"%ld",btn.tag);}

,不论你在什么时候结束,重要的是结束之後就不要悔恨

iOS for循环创建button,button的宽度根据上面的文字来自适应.

相关文章:

你感兴趣的文章:

标签云: