gpt4 book ai didi

objective-c - 标题中有两行文本的 UIButton (numberOfLines=2)

转载 作者:IT老高 更新时间:2023-10-28 11:23:07 30 4
gpt4 key购买 nike

我正在尝试制作一个 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 来保存文本并将其添加为按钮的 subview 的唯一解决方案吗?

最佳答案

您不需要向 UIButton 添加 UILabel。这只是额外的对象和工作。

在按钮的 titleLabel 上设置这些属性。

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2;//if you want unlimited number of lines put 0

swift :

button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
button.titleLabel!.numberOfLines = 2//if you want unlimited number of lines put 0

关于objective-c - 标题中有两行文本的 UIButton (numberOfLines=2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7831928/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com