最佳答案
我试图创建一个 UIButton
,它的 title Label 中有两行文本。这是我正在使用的代码:
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];
当我尝试这样做时,文本只出现在一行上。在 UIButton.titleLabel
中实现多行文本的唯一方法似乎是设置 numberOfLines=0
并使用 UILineBreakModeWordWrap
。但这并不能保证文本正好是两行。
然而,使用普通的 UILabel
确实有效:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];
有人知道如何让 UIButton
用两条线工作吗?创建一个单独的 UILabel
来保存文本,并将其添加为按钮的子视图,这是唯一的解决方案吗?