我正在尝试使UIButton
titleLabel中包含两行文本。这是我正在使用的代码:
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
的文本来保存文本并将其添加为按钮的子视图的唯一解决方案吗?