我在试着做一个UIButton
,它的标题标签里有两行文字.这是我使用的代码:
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];
当我try 这样做时,文本只出现在一行上.似乎在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
来保存文本,并将其添加为按钮的子视图吗?