码迷,mamicode.com
首页 > 其他好文 > 详细

Button 自动换行

时间:2015-08-06 13:11:30      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 300)];

    view.backgroundColor=[UIColor grayColor];

    [self.view addSubview:view];

    NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:@"园园",@"园园",@"蛋蛋园园",@"啊啊啊啊",@"拉",@"呀呀呀啊呀啊呀呀", nil];

    

    int i=0;

    CGFloat btnH=30;

    CGFloat yOffset=10;

    CGFloat xOffset=15;

    for (NSString *string in array) {

      

        CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, btnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17] } context:nil];

        

        CGFloat w = rect.size.width+10;

        NSLog(@"%f",yOffset+w);

        if (xOffset+w>self.view.frame.size.width-20) {

            xOffset=10;

            yOffset+=(btnH+15);

        }

        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

        button.titleLabel.font=[UIFont systemFontOfSize:17];

        [button setTitle:string forState:(UIControlStateNormal)];

        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

        //自己 加  Button的点击 事件 

        [button setBackgroundColor:[UIColor whiteColor]];

        button.layer.cornerRadius=5;

        button.clipsToBounds=YES;

        button.tag=1000+i;

        button.frame = CGRectMake(xOffset, yOffset, w, btnH);

        [view addSubview:button];

         xOffset+=(w+10);

         ++i;

    }

 

Button 自动换行

标签:

原文地址:http://www.cnblogs.com/106dapeng/p/4707524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!