根据文本内容调整Button大小

    xiaoxiao2023-03-24  4

    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);
    转载请注明原文地址: https://ju.6miu.com/read-1201464.html
    最新回复(0)