根据文本内容调整Button大小

来源:互联网 发布:js if语句的嵌套 编辑:程序博客网 时间:2024/06/06 21:04
CGFloat height = self.scrollView.bounds.size.height;    CGFloat btnX = 5;    for (int index = 0; index < dataSource.count; index ++) {        UIButton *button = [[UIButton alloc] init];        [self.scrollView addSubview:button];        [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];        button.titleLabel.font = [UIFont systemFontOfSize:16];        NSString *title = dataSource[index];        [button setTitle:title forState:UIControlStateNormal];        //计算宽        CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:button.titleLabel.font.fontName size:button.titleLabel.font.pointSize]}];    CGFloat    width = titleSize.width+10;        button.frame = CGRectMake(btnX, 0, width, height);        [button addTarget:self action:@selector(categoriesBtnAction:) forControlEvents:UIControlEventTouchUpInside];        btnX += button.frame.size.width;    }    self.scrollView.contentSize = CGSizeMake(btnX, height);
0 0
原创粉丝点击