iOS 多个不同宽度button布局自动换行
来源:互联网 发布:修改mysql配置文件 编辑:程序博客网 时间:2024/05/15 00:28
mark下需要循环布局不同宽度button的情况,根据button宽度自动换行。
#pragma mark - 多个不同宽度button自动换行-(void)createViewWithTitleArr{ UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 250, WID, 45)]; bgView.backgroundColor = [UIColor cyanColor]; [self.view addSubview:bgView]; NSArray *titleAry = @[@"安徽发克鲁斯", @"啥就开始打开", @"等哈说楼房吉安老师发货啦", @"盛开的花", @"就发生;楼房吉安;十分骄傲;十分骄傲;是否"]; CGFloat pointX = 10.0; //button X坐标 CGFloat pointY = 10.0; //button Y坐标 CGFloat padding = 5.0; //button 间距 CGFloat btnHeight = 30; //button高度 CGFloat allWidth = WID - 10; UIFont *titleFont = [UIFont systemFontOfSize:15]; for (int i = 0; i < titleAry.count; i++) { CGRect rect = [titleAry[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont} context:nil]; CGFloat btnWidth = rect.size.width + 20; if (pointX + btnWidth > allWidth) {//换行 pointX = 10;//X从新开始 pointY += (btnHeight + padding);//换行后Y+ } UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom]; but.frame = CGRectMake(pointX, pointY, btnWidth, btnHeight); but.tag = i + 1000; [but addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside]; but.layer.masksToBounds = YES; but.layer.cornerRadius = btnHeight/2; but.layer.borderWidth = 1; but.layer.borderColor = [UIColor magentaColor].CGColor; [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [but setTitle:titleAry[i] forState:UIControlStateNormal]; but.titleLabel.font = titleFont;//一定要一样 pointX += (btnWidth + padding);//每次X都加上button宽和间距5 [bgView addSubview:but]; } CGRect rect2 = bgView.frame; rect2.size.height = pointY + btnHeight + 10; bgView.frame = rect2;}-(void)clickButtonAction:(UIButton *)sender{ if (sender.selected) { return; } _tempButton.selected = NO; _tempButton.backgroundColor = [UIColor clearColor]; sender.selected = YES; sender.backgroundColor = [UIColor magentaColor]; _tempButton = sender;}
阅读全文
0 0
- iOS 多个不同宽度button布局自动换行
- iOS 多个label/button自动换行
- 标签布局,实现添加多个button自动换行,可自定义许多属性
- IOS 开发使用UITableView实现自动布局多个button 按钮
- button自适应宽度 并根据屏幕宽自动换行排列
- 自定义控件-MultipleTextView(自动换行、自动补齐宽度的排列多个TextView)
- 自动换行的button
- 自动换行的button
- button 自动换行
- ios之动态创建多个标签,自动换行
- iOS label自适应宽度 左端对齐 自动换行
- 自定义自动换行布局控件,实现子控件宽度超出整行时自动换行。setMeasuredDimension的使用
- 自定义自动换行布局控件,实现子控件宽度超出整行时自动换行。setMeasuredDimension的使用
- li 自适应宽度 自动换行
- iOS根据标签不同长度内容按钮自动换行
- 按钮布局--自动换行
- 自动换行子布局
- CSS网页布局中DIV和TABLE超出宽度自动换行分析
- 多线程概念
- C++ 斐波拉契 河内塔
- 为RecyclerView添加吸顶条目分组(ItemDecoration)
- 机器学习
- Session 'TestApp': Error Installing APKs以及gradle building running问题
- iOS 多个不同宽度button布局自动换行
- 端口分类
- JavaScript——正则表达式大全
- Java静态内部类特性详解
- MapReduce中的二次排序
- Nginx nginx.conf配置文件详解
- python学习整理
- 机器学习项目开发实战,应用
- linux下gdb调试基本命令学习