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

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

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

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

/***********************.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欢迎您",@"北您",@"北京是打发撒旦双方的飞洒欢迎您",@"北京阿斯蒂芬迎您",@"北迎您",];        //创建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),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迎您",];    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

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的iOS自动适应多个宽度不同的按钮 自动换行排布全部内容,希望文章能够帮你解决iOS自动适应多个宽度不同的按钮 自动换行排布所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1107172.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存