【发布时间】:2011-12-11 12:40:41
【问题描述】:
我正在尝试制作一个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 来保存文本,并将其添加为按钮的子视图?
【问题讨论】:
-
你见过这个吗? - stackoverflow.com/questions/604632/…
-
Viraj,是的,如果您设置
numberOfLines=0并使用UILineBreakModeWordWrap,您可以获得多行。这样做的问题是,如果文本太长,它可能会给出两行以上。我想要正好在第二行末尾带有省略号的两个单独的字符(如果文本太长)。 -
哦,那我想添加子视图可能是唯一的方法了。
标签: objective-c ios uibutton uilabel