iOS自动适应多个宽度不同的按钮 自动换行排布

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

/***********************.m文件********************************/

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) NSMutableArray *interestArray;/*存放选中的按钮*/

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.interestArray = [NSMutableArray new];
        [self viewtwo];
//      [self viewView];
    [self viewView];
    
}
//第一种方法
- (void)viewView{
    UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    view.backgroundColor = [UIColor yellowColor];
        int width = 0;
        int height = 0;
        int number = 0;
        int han = 0;
        
        NSArray *titleArr = @[@"北京欢sdf迎您",@"北fads京欢迎您",@"北京sdfasas欢迎您",@"北京sdfasf欢迎您",@"北京sdfa您",@"北京sdfsaf欢迎您",@"北京sdfsaf欢迎您",@"北您",@"北京是打发撒旦双方的飞洒欢迎您",@"北京阿斯蒂芬迎您",@"北京sdfsaf欢迎您",@"北迎您",];
        //创建button
        for (int i = 0; i < titleArr.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 300 + i;
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            CGSize titleSize = [self getSizeByString:titleArr[i] AndFontSize:20];
            han = han +titleSize.width;
            if (han > [[UIScreen mainScreen]bounds].size.width) {
                han = 0;
                han = han + titleSize.width;
                height++;
                width = 0;
                width = width+titleSize.width;
                number = 0;
                button.frame = CGRectMake(10, 10 +50*height, titleSize.width, 40);
            }else{
                button.frame = CGRectMake(width+10+(number*10), 10 +50*height, titleSize.width, 40);
                width = width+titleSize.width;
            }
            number++;
            button.titleLabel.font = [UIFont systemFontOfSize:13];
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 10;
            button.backgroundColor = [UIColor lightGrayColor];
            button.titleLabel.font = [UIFont systemFontOfSize:20];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setTitle:titleArr[i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:button];
        }
    
    [self.view addSubview:view];
    
}
//第二种方法
- (void)viewtwo{
    UIView * tagView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height)];
    //第一个 label的起点
    CGSize size = CGSizeMake(5, 30);
    
    //间距
    CGFloat padding = 5.0;
    
    CGFloat width = [UIScreen                                                                       mainScreen].bounds.size.width;
    
    NSArray *titleArr = @[@"北京欢sdf迎您",@"北fads京欢迎您",@"北京sdfasas欢迎您",@"北京sdfasf欢迎您",@"北京sdfa您",@"北京sdfsaf欢迎您",@"北京sdfsaf欢迎您",@"北您",@"北京是打发撒旦双方的飞洒欢迎您",@"北京阿斯蒂芬迎您",@"北京sdfsaf欢迎您",@"北迎您",];
    for (int i = 0; i < titleArr.count; i ++) {
        
        CGFloat keyWorldWidth = [self getSizeByString:titleArr[i] AndFontSize:14].width;
        if (keyWorldWidth > width) {
            keyWorldWidth = width;
        }
        if (width - size.width < keyWorldWidth) {
            size.height += 30.0;
            size.width = 5.0;
        }
        //创建 label点击事件
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(size.width, size.height-30, keyWorldWidth, 20)];
        button.titleLabel.numberOfLines = 0;
        button.backgroundColor = [UIColor redColor];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.layer.cornerRadius = 3.0;
        button.layer.masksToBounds = YES;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setTitle:titleArr[i] forState:UIControlStateNormal];
        [tagView addSubview:button];
        button.tag = i;
        [button addTarget:self action:@selector(tagButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        //起点 增加
        size.width += keyWorldWidth+padding;
    }
    [self.view addSubview:tagView];
}


- (void)handleButton:(UIButton *)button{
    button.selected = !button.selected;
    if (button.selected) {
        
        button.backgroundColor = [UIColor redColor];
        [self.interestArray addObject:button.titleLabel.text];
        
    }else{
        
        button.backgroundColor = [UIColor lightGrayColor];
        [self.interestArray removeObject:button.titleLabel.text];
    }
    

}
- (void)tagButtonClick:(UIButton *)button{

    NSLog(@"%@",button.titleLabel.text);
}
//计算文字所占大小
- (CGSize)getSizeByString:(NSString*)string AndFontSize:(CGFloat)font
{
    CGSize size = [string boundingRectWithSize:CGSizeMake(999, 25) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil].size;
    size.width += 5;
    return size;
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end