iOS 根据文本内容调整Button大小

释放双眼,带上耳机,听听看~!
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);

人已赞赏
iOS文章

iOS UIImage使用

2020-2-22 12:55:29

iOS文章

iOS parallaxView实现方式总结-个人中心背景效果

2020-2-22 14:24:52

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索