我正在以编程方式向我的视图添加一个 UIButton,我希望按钮的字体大小能够自动调整(例如,如果文本很长,则将其调整为较小的字体以适合按钮)。
下面的代码无效(字体始终相同):
myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[myButton setTitle:[NSString stringWithFormat:@"hello"] forState:UIControlStateNormal];
[myButton setFrame: CGRectMake(0, 0, 180, 80)];
[myButton.titleLabel setFont: [UIFont boldSystemFontOfSize:16.0]];
myButton.titleLabel.adjustsFontSizeToFitWidth = TRUE;
[theView addSubview:myButton];
sizeToFit
方法。 - danielbeard