带尾部截断的 UIButton 多行文本

发布于 2024-10-25 14:54:54 字数 379 浏览 4 评论 0原文

我发现类似的问题询问如何在 UIButton 上显示多行文本,解决方案是设置

[myUIButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[myUIButton setTitle:myTitle forState:UIControlStateNormal];

然而,这会导致按钮标题占用多行。我尝试限制使用的行数

[myUIButton.titleLabel setNumberOfLines:2];

,但这对结果行数没有任何影响。

有没有办法将 UIButton 标题上的行字限制为 2 行,然后用“...”截断尾部?

I have found similar questions that ask how to have multi-line text on a UIButton, and the solution is to set

[myUIButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[myUIButton setTitle:myTitle forState:UIControlStateNormal];

However, this results in the button title taking up many lines. I have tried restricting the number of lines using

[myUIButton.titleLabel setNumberOfLines:2];

but this does not have any affect on the resulting number of lines.

Is there a way to limit the lines word wrapped to 2 lines on the UIButton title, and then have the tail truncated with "..."?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒解孤独 2024-11-01 14:54:54

通过在 numberOfLines 之前设置 lineBreakMode 可以实现您想要的结果......
这是因为 lineBreakMode 似乎取消了 numberOfLines 设置,因此我们按此顺序执行。

Objective-C:

[button.titleLabel setLineBreakMode: UILineBreakModeTailTruncation];
[button.titleLabel setNumberOfLines:2];
[button setTitle:myTitle forState:UIControlStateNormal];

Swift 3:
从 Xcode 6 及更高版本开始,UILineBreakModeNSLineBreakMode 替换

button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
button.titleLabel?.numberOfLines = 2
button.setTitle(myTitle, for: UIControlState.normal)

By setting lineBreakMode before numberOfLines your desired result can be achieved…
This is because lineBreakMode seems to cancel out numberOfLines set hence we are doing it in this order.

Objective-C:

[button.titleLabel setLineBreakMode: UILineBreakModeTailTruncation];
[button.titleLabel setNumberOfLines:2];
[button setTitle:myTitle forState:UIControlStateNormal];

Swift 3:
from Xcode 6 and above UILineBreakMode is replaced by NSLineBreakMode

button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
button.titleLabel?.numberOfLines = 2
button.setTitle(myTitle, for: UIControlState.normal)
余生共白头 2024-11-01 14:54:54

我知道自从第一次提出这个问题以来已经有一段时间了,但我遇到了同样的问题,并在考虑发布的答案后最终得到了一个简单但实​​用的解决方案
此处。

解决方案对我有用的是以下内容:

// Set the line break mode to word wrap so it won't truncate automatically
[button.titleLabel setLineBreakMode: UILineBreakModeWordWrap];

// Call a method that truncates the string I want to use
[button setTitle:[self truncateString:myButtonText] forState:UIControlStateNormal];

和 truncateString 方法:

- (NSString *)truncateString:(NSString *)stringToTruncate
{
    if ([stringToTruncate length] > 50)
        stringToTruncate = [[stringToTruncate substringToIndex:50] stringByAppendingString:@"..."];

    return  stringToTruncate;
}

所以基本上我计算了适用于我的按钮的字符数,然后强制任何比该长度长的字符串末尾带有“...”。我知道这不是理想的解决方案,但我想它对我们中的一些人有用,我希望它有所帮助。

I know its been a while since the question was first asked, but I came across the same problem and endend up with a simple but functional solution after considering the answer posted
here.

The solution that worked for me was the following:

// Set the line break mode to word wrap so it won't truncate automatically
[button.titleLabel setLineBreakMode: UILineBreakModeWordWrap];

// Call a method that truncates the string I want to use
[button setTitle:[self truncateString:myButtonText] forState:UIControlStateNormal];

And the truncateString method:

- (NSString *)truncateString:(NSString *)stringToTruncate
{
    if ([stringToTruncate length] > 50)
        stringToTruncate = [[stringToTruncate substringToIndex:50] stringByAppendingString:@"..."];

    return  stringToTruncate;
}

So basically I calculated the number of characters that would work for my button, and then forced any string longer than that to have the '...' at the end. I know its not the ideal solution but I guess it can work for some of us, I hope it helps.

你穿错了嫁妆 2024-11-01 14:54:54
[button.titleLabel setNumberOfLines:2];
[button.titleLabel setLineBreakMode: UILineBreakModeTailTruncation];
[button setTitle:myTitle forState:UIControlStateNormal];

它对我有用! :D
干杯!!

[button.titleLabel setNumberOfLines:2];
[button.titleLabel setLineBreakMode: UILineBreakModeTailTruncation];
[button setTitle:myTitle forState:UIControlStateNormal];

it does it for me! :D
cheers!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文