我有如下代码:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 25, 25);
[[button layer] setCornerRadius:5.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBackgroundColor:[[UIColor redColor] CGColor]];
[button.titleLabel setFrame:CGRectMake(0,0, 25, 25)];
[button setTitle:[NSString stringWithFormat:@"%@", [[topics objectAtIndex:indexPath.row] unread]] forState:UIControlStateNormal];
问题在于当文本字符串不太长时,它可以正常显示(1-2位数字),但是当长度相当长(3个或更多数字)时,我只能看到一个红色按钮,里面没有文字。我该如何调整呢?我认为:
[button.titleLabel setAdjustsFontSizeToFitWidth:YES];
工作正常,对吗?
titleButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
。 - onmyway133