android scrollview 自动换行,iOS UIScrollView上添加button,根据按钮的宽度自动换行

UIScrollView * buttonView = [[UIScrollView alloc]init];

buttonView.showsHorizontalScrollIndicator = NO;

buttonView.showsVerticalScrollIndicator = NO;

buttonView.backgroundColor= [UIColorgreenColor];

[whightViewaddSubview:buttonView];

CGFloatpointX =16.0;

CGFloatpointY =5;

CGFloatallWidth =self.frame.size.width-20;

UIFont*titleFont = [UIFontsystemFontOfSize:15];

for(inti =0; i <12; i++) {

//        CGRect rect = [self.CardArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, 25) self.CardArray.count options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont} context:nil];

CGFloat  width =140;//rect.size.width + 30;

if(pointX + width > allWidth) {//换行

pointX =16;//X从新开始

pointY +=40;//换行后Y+

}

UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];

but.frame=CGRectMake(pointX, pointY, width,32);

but.tag= i +1000;

[butaddTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];

but.layer.masksToBounds = YES;

but.layer.cornerRadius=10;

but.backgroundColor = [UIColor redColor];

[butsetTitleColor:[UIColor blackColor ] forState:UIControlStateNormal];

//        [but setTitle:self.CardArray[i] forState:UIControlStateNormal];

but.titleLabel.font= titleFont;//一定要一样

pointX += (width +5);//每次X都加上button宽和间距5

[buttonViewaddSubview:but];

}

if(cardArray.count<10) {

buttonView.frame=CGRectMake(0, titleLabel.Y+titleLabel.height+10,self.width,140);

buttonView.contentSize=CGSizeMake(self.width, pointY +35);

}else{

buttonView.frame=CGRectMake(0, titleLabel.Y+titleLabel.height+10,self.width,180);

buttonView.contentSize=CGSizeMake(self.width, pointY +35);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值