NSArray
*arr =
@[
@"
无知
"
,
@"
风云变幻
"
,
@"
施耐庵
"
,
@"
唉
"
,
@"
西门吹雪
"
,
@"
呵呵哒
"
,
@"
快看看
"
,
@"
窿窿啦啦
"
,
@"
一杆禽兽狙
"
,
@"
合欢花
"
,
@"
暴走大事件
"
,
@"
非诚勿扰
"
,
@"
呵呵呵
"
]
;
CGFloat w = 0 ; // 保存前一个 button 的宽以及前一个 button 距离屏幕边缘的距离
CGFloat h = 100 ; // 用来控制 button 距离父视图的高
for ( int i = 0 ; i < arr. count ; i++) {
UIButton *button = [ UIButton buttonWithType : UIButtonTypeSystem ];
button. tag = 100 + i;
button. backgroundColor = [ UIColor colorWithRed : 239 / 255.0 green : 239 / 255.0 blue : 244 / 255.0 alpha : 1 ];
[button addTarget : self action : @selector (handleClick:) forControlEvents : UIControlEventTouchUpInside ];
[button setTitleColor :[ UIColor colorWithWhite : 0.400 alpha : 1.000 ] 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. layer . cornerRadius = 4 ;
button. layer . masksToBounds = YES ;
// 当 button 的位置超出屏幕边缘时换行 320 只是 button 所在父视图的宽度
if ( 10 + w + length + 15 > 355 ){
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];
}
CGFloat w = 0 ; // 保存前一个 button 的宽以及前一个 button 距离屏幕边缘的距离
CGFloat h = 100 ; // 用来控制 button 距离父视图的高
for ( int i = 0 ; i < arr. count ; i++) {
UIButton *button = [ UIButton buttonWithType : UIButtonTypeSystem ];
button. tag = 100 + i;
button. backgroundColor = [ UIColor colorWithRed : 239 / 255.0 green : 239 / 255.0 blue : 244 / 255.0 alpha : 1 ];
[button addTarget : self action : @selector (handleClick:) forControlEvents : UIControlEventTouchUpInside ];
[button setTitleColor :[ UIColor colorWithWhite : 0.400 alpha : 1.000 ] 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. layer . cornerRadius = 4 ;
button. layer . masksToBounds = YES ;
// 当 button 的位置超出屏幕边缘时换行 320 只是 button 所在父视图的宽度
if ( 10 + w + length + 15 > 355 ){
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];
}
原文:http://blog.csdn.net/duoduo_333/article/details/47024551