标签状态栏文字自适应排布

记录一下简单的标签状态栏,并且文字自适应屏幕。一种是加在scrollview上面,一种是根据屏幕大小换行的。

先看第一种:加在scrollView上的布局

标签状态栏文字自适应排布_第1张图片


第二种:根据屏幕换行

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的frame  

button.frame = CGRectMake(10 + w, h, length + 15 , 30);  

//当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度  

if(10 + w + length + 15 > 320){  

w =0; //换行时将w置为0  

h = 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];  


标签状态栏文字自适应排布_第2张图片

你可能感兴趣的:(标签状态栏文字自适应排布)