UILabel 与 UILineBreakModeWordWrap 的结果行
我有一个 UILabel,其大小是使用 sizeWithFont:
方法计算的。换行模式设置为 UILineBreakModeWordWrap
(使用 sizeWithFont:
计算大小时使用相同的标志)...
一切都很好,标签大小正确并显示我的文本根据需要。
现在我需要知道用于显示标签的行(或使用 sizeWithFont:
时生成的行)。从技术上讲,我可以根据空格/插入符返回编写自己的换行实现,但是不能保证它与 Apple 的实现方式相同,因此生成的行将不是用于计算文本大小的行,不要介意重新发明轮子的事实。
理想情况下,我会传递字符串,指定宽度和换行模式,并接收表示文本可视行的字符串数组。
有什么想法如何以最优雅的方式实现这一点吗?
I have a UILabel whose size is calculated with sizeWithFont:
method. The line break mode is set to UILineBreakModeWordWrap
(same flag is used when calculating the size with sizeWithFont:
)...
Everything works great, label is properly sized and displays my text as required.
Now I need to know the lines that are used to display the label (or the lines that are generated when sizeWithFont:
is used). I could technically write my own implementation of line breaking based on spaces/caret returns, but then it's not going to be guaranteed the same way as Apple's implementation and hence the resulting lines will not be the ones that are used to calculate the size of text, nevermind the fact of reinventing the wheel.
Ideally, I would pass my string, specify the width and line break mode and receive an array of strings representing the visual lines of text.
Any ideas how to make this happen in the most elegant way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要计算
UILabel
在换行文本后的行数,您需要找到UILabel
字体的前导(行高)(label .font.leading
),然后将多行UILabel
的高度除以每行的高度即可得出行数。这是一个例子:
或者,你可以用两行来完成:
To calculate the number of lines that a
UILabel
has after wrapping it's text you will need to find the leading (line height) of yourUILabel
's font (label.font.leading
) and then divide the height of your multi-lineUILabel
by the height of each line to yield the number of lines.Here's an example:
Or, you could do it in two lines:
我认为这没有什么灵丹妙药。
这是一个类别方法,似乎适用于我扔给它的几个基本测试用例。不保证它不会因为复杂的东西而崩溃!
它的工作方式是通过字符串测试来查看一系列单词是否适合标签的宽度。当它计算出当前范围太宽时,它会将最后拟合的范围记录为一条线。
我并不认为这是有效的。更好的方法可能就是实现你自己的 UILabel...
使用如下:
I don't think there is any silver bullet for this.
Here is a category method that seems to work for the few basic test cases I threw at it. No guarantees it won't break with something complex!
The way it works is to move through the string testing to see if a range of words fits in the width of the label. When it calculates that the current range is too wide it records the last-fitting range as a line.
I don't claim this is efficient. A better way may just to be to implement your own UILabel...
use like this:
只需调用下面的方法并传递
UILabel
或UITextView
:现在调用此方法:-
更新:SWIFT 代码
< strong>现在调用此方法:-
注意:
leading
- 使用 lineHeight。不返回实际领先。将来将被正式弃用。Just call below method and pass either
UILabel
orUITextView
:Now call this method:-
UPDATED: SWIFT CODE
Now call this method:-
Note:
leading
- use lineHeight. does not return actual leading. will be formally deprecated in future.对于 Xcode 7 及更高版本,TheTiger 的答案需要对以下代码进行更新注释:
这仅在您使用相同的字体和大小时才有效,这不是一个明智的举动,但它帮助了我,我想将其作为我知道的当前解决方案进行分享
for Xcode 7 and up TheTiger's answer needs an update commented on the code below :
this will only work if you are using same font and size , it's not a smart move but it helped me and i wanted to share it as the current solution i know
更新为 Swift 3
要计算 UILabel 的行数,在换行文本后,您需要将多行 UILabel 的高度除以每行的高度 (
ascender< /代码>)。
当尝试阿道夫的答案时,由于某种原因,
label.font.leading
返回0.0,所以我使用label.font.ascender
,它返回从基线到顶部的高度UILabel
的框架。见下图。Updated for Swift 3
To calculate the number of lines that UILabel has, after wrapping its text, you need to divide the height of your multi-line UILabel by the height of each line (
ascender
).When trying Adolfo's answer, for some reason,
label.font.leading
returned 0.0, so I usedlabel.font.ascender
, which returns the height from the baseline to the top of theUILabel
's frame. See picture below.