从 UILabel 获取截断的文本
无论如何,我可以获得 UILabel 文本的截断版本吗?
简而言之,我有一段文本和两个 UILabels - 标签 A(2 行长)和标签 B(高度可变)。标签 A 位于标签 B 之上。其想法是,标签 A 显示文本段落的前两行,并且在某个用户操作后,标签 B 可见并显示文本的其余部分。
我无法确定标签 B 中应包含哪些内容,因为我不知道标签 A 中显示的内容。我还需要从标签 A 中删除“...”。
注意:我意识到这是一个有点令人费解,但有一些很好的理由,我不会用这些理由来混淆这个问题。
Is there anyway I can get the truncated version of the text for a UILabel?
In short, I have a paragraph of text, and two UILabels
- label A, which is 2 lines long, and label B, which is a variable height. Label A is above label B. The idea is that label A shows the first two lines of the paragraph of text, and upon a certain user action, label B because visible and displays the rest of the text.
I'm having trouble determining what should go in label B, as I don't know what's being shown in label A. I'd need to also remove the "..." from label A.
Note: I realize this is a bit convoluted but there are some good reasons for it, which I won't clutter up the question with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想知道您是否可以使用 中的方法NSString UIKit Additions 来计算标签 A 的适合程度。
一种粗略的方法可能是从文本的第一个字符开始并测试它所占用的大小(
-sizeWithFont:forWidth:lineBreakMode:
也许?)然后继续一次添加一个字符,直到它不再适合您的标签 A 为止。我希望其他人能想出更好的方法来做到这一点,但上述应该可行。
更新
昨晚,我对自己的应用程序的 Core Text 进行了一些研究,发现了
CTFramesetterSuggestFrameSizeWithConstraints
。您可以通过查看该函数中的fitRange
来使用它来确定标签适合多少字符串。更新 2:
我认为这应该可行,但我刚刚在此处输入了此内容,因此它甚至可能无法编译:
I wonder if you could use the methods in the NSString UIKit Additions to figure out how much fits into label A.
A crude way might be to start with the first character of your text and test for the size it would take up (
-sizeWithFont:forWidth:lineBreakMode:
maybe?) and then keep adding characters one at a time until it doesn't fit into your label A any more.I hope somebody else can come up with a better way to do this, but the above should work.
Update
Last night I looked a bit into Core Text for my own app and came across
CTFramesetterSuggestFrameSizeWithConstraints
. You could maybe use this to figure out how much of your string fits into the label, by looking at thefitRange
in that function.Update 2:
I think this should work, but I have just typed this in here, so it may not even compile:
感谢托马斯·穆勒
请务必将 myLabel 的换行模式设置为:
通过此方法,您可以获得实际适合约束大小的分块字符串。
这是烘焙的代码:
thanks to Thomas Müller
be sure to set line break mode the myLabel to this:
by this method you can get chunked strings that actually fit in the constrained size.
Here is the baked code:
对于标签 A,对于您正在使用的特定字体,计算应完美适合两行的近似字符。
对于标签 B,设置整个文本必须适合的变量高度。
For Label A, calculate approximate character that should fit perfectly for two lines, for the particular font you are using.
For label B, set variable Height that the whole text must fit into it.