Objective C - UILabel 多行垂直间隙高度
我有一个多行 UILabel,最多可以包含 3 行。 即 message.numberOfLines = 3;
一切正常,但如何设置线之间的垂直间隙? 例如1号线和2号线之间等等?
请赐教,
T恤
I have a multiline UILabel that can take a maximum of 3 lines.
i.e message.numberOfLines = 3;
Everything works fine, but how can I set the vertical gap between the lines?
e.g between line 1 and line 2 etc?
Please enlight,
Tee
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我一直无法找到调整行间距的方法。 UILabel 的 font 属性有许多只读属性,因此没有帮助。
如果我想更改行间距,我会求助于绘制自己的文本。我使用 NSString 的 -drawAtPoint 和 -drawInRect 并使用一个或多个 -sizeWithFont 方法来计算文本的长度,以便分割文本并在每行绘制正确数量的单词或字符。
I haven't been able to find a way to adjust the spacing between lines. The font property of UILabel has a number of read-only properties, so that won't help.
I've resorted to draw my own text if I want to change the line spacing. I use NSString's -drawAtPoint and -drawInRect and use one or more of the -sizeWithFont methods to figure out how long the text will be in order to split the text and draw the right number of words or characters per line.
不幸的是,据我所知,执行此操作的唯一好方法是自己绘制文本。在 3.2 或更高版本上,您可以使用 CoreText 直接绘制文本 - 我不确定 CT 是否在 iPhone >= 4.0 上可用。 CoreText 是一个用于灵活文本绘制的函数式 C API。
Unfortunately, the only good way to do this that I'm aware of is to draw the text yourself. On 3.2 or later, you can use CoreText to draw the text directly - I'm not sure if CT is available on iPhones >= 4.0. CoreText is a functional C API for flexible text drawing.
以编程方式:
SWift 4
使用标签扩展
现在调用扩展函数
或使用标签实例(只需复制并执行此代码即可查看结果)
从 Interface Builder:
Programmatically:
SWift 4
Using label extension
Now call extension function
Or using label instance (Just copy & execute this code to see result)
From Interface Builder: