iOS根据标签不同长度内容按钮自动换行

//

//  ViewController.m

//  tableview

//

//  Created by 方超 on 16/6/14.

//  Copyright © 2016方超. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic,strong)UITableView * tableview;

@property(nonatomic,strong)NSArray * array;

@property(nonatomic,assign)float cellheight;


@end




@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.tableview=[[UITableViewalloc]initWithFrame:self.view.bounds];

    self.tableview.delegate=self;

    self.tableview.dataSource=self;

    [self.viewaddSubview:self.tableview];

    

    self.array=@[@"11122",@"safaf",@"事实上事实上",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"",@"121",@"121"];

    

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{


    return1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    returnself.array.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    returnself.cellheight;

}

- (CGSize)getSizeByString:(NSString*)string AndFontSize:(CGFloat)font

{

    CGSize size = [stringboundingRectWithSize:CGSizeMake(999,25) options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:font]}context:nil].size;

    size.width +=5;

    return size;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    staticNSString * strcell=@"cellstr";

    UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:strcell];

    if (!cell){

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:strcell];

    }

    

    int width =0;

    int height =0;

    int number =0;

    int han =0;

    for (int i =0; i < self.array.count; i++){

        UILabel *button = [[UILabelalloc]initWithFrame:CGRectMake(0,15, 50,20)];

        button.tag =300 + i;

        button.textAlignment=NSTextAlignmentCenter;

        CGSize titleSize = [selfgetSizeByString:self.array[i]AndFontSize:13];

        

        han = han +titleSize.width;

        if (han >= [[UIScreenmainScreen]bounds].size.width-75) {

            han = 0;

            han = han + titleSize.width;

            height++;

            width = 0;

            width = width+titleSize.width;

            number = 0;

            button.frame =CGRectMake(11,30 +25*height, titleSize.width,20);

            self.cellheight=button.frame.origin.y;

            

        }else{

            button.frame =CGRectMake(width+11+(number*10),30 +25*height, titleSize.width,20);

            width = width+titleSize.width;

            self.cellheight=button.frame.origin.y;

            

        }

        number++;

        button.enabled=NO;

        button.font = [UIFontsystemFontOfSize:11];

        button.layer.masksToBounds =YES;

        button.layer.cornerRadius =5;

        button.textColor=[UIColorwhiteColor];

        button.text=[self.arrayobjectAtIndex:i];

        

        [button.layersetBorderWidth:1];

        

        //设置按钮的边界颜色

        

        CGColorSpaceRef colorSpaceRef =CGColorSpaceCreateDeviceRGB();

        

        CGColorRef color =CGColorCreate(colorSpaceRef, (CGFloat[]){0.5,0.5,0.5,0.5});

        

        [button.layersetBorderColor:color];

        

        [cell addSubview:button];

    

    }


    return cell;


}

    


@end












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值