带尾部截断的 UIButton 多行文本
我发现类似的问题询问如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过在
numberOfLines
之前设置lineBreakMode
可以实现您想要的结果......这是因为
lineBreakMode
似乎取消了numberOfLines
设置,因此我们按此顺序执行。Objective-C:
Swift 3:
从 Xcode 6 及更高版本开始,
UILineBreakMode
被NSLineBreakMode
替换By setting
lineBreakMode
beforenumberOfLines
your desired result can be achieved…This is because
lineBreakMode
seems to cancel outnumberOfLines
set hence we are doing it in this order.Objective-C:
Swift 3:
from Xcode 6 and above
UILineBreakMode
is replaced byNSLineBreakMode
我知道自从第一次提出这个问题以来已经有一段时间了,但我遇到了同样的问题,并在考虑发布的答案后最终得到了一个简单但实用的解决方案
此处。
解决方案对我有用的是以下内容:
和 truncateString 方法:
所以基本上我计算了适用于我的按钮的字符数,然后强制任何比该长度长的字符串末尾带有“...”。我知道这不是理想的解决方案,但我想它对我们中的一些人有用,我希望它有所帮助。
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:
And the truncateString method:
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.
它对我有用! :D
干杯!!
it does it for me! :D
cheers!!