设置 UILabel 行距
如何修改多行 UILabel 中的行间距(行间距)?
How can I modify the gap between lines (line spacing) in a multiline UILabel
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何修改多行 UILabel 中的行间距(行间距)?
How can I modify the gap between lines (line spacing) in a multiline UILabel
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
编辑:显然,在 iOS 6 及更高版本上,
NSAttributedString
可以做到这一点。不要使用NSString
来设置标签的文本,而是创建一个NSAttributedString
,在其上设置属性,然后将其设置为标签上的.attributedText
标签。您想要的代码将是这样的:NSAttributedString 的旧 attributeStringWithString 做了同样的事情,但现在已被弃用。
由于历史原因,这是我原来的答案:
简短回答:你不能。要更改文本行之间的间距,您必须子类化
UILabel
并推出您自己的drawTextInRect
、创建多个标签或使用不同的字体(可能是为某个版本编辑的字体)具体行高见Phillipe的回答)。长答案:在印刷和在线世界中,文本行之间的空格被称为“前导”(与“标题”押韵,来自几十年前使用的铅金属)。 leading 是 UIFont 的只读属性,在 4.0 中已弃用并由 lineHeight 取代。据我所知,没有办法用一组特定的参数(例如lineHeight)创建字体;您可以获得系统字体和添加的任何自定义字体,但安装后无法对其进行调整。
UILabel
中也没有间距参数。我对
UILabel
的行为不太满意,因此我建议编写您自己的子类或使用第三方库。这将使行为独立于您的字体选择,并成为最可重用的解决方案。我希望 UILabel 具有更大的灵活性,并且我很高兴被证明是错误的!
Edit: Evidently
NSAttributedString
will do it, on iOS 6 and later. Instead of using anNSString
to set the label's text, create anNSAttributedString
, set attributes on it, then set it as the.attributedText
on the label. The code you want will be something like this:NSAttributedString's old attributedStringWithString did the same thing, but now that is being deprecated.
For historical reasons, here's my original answer:
Short answer: you can't. To change the spacing between lines of text, you will have to subclass
UILabel
and roll your owndrawTextInRect
, create multiple labels, or use a different font (perhaps one edited for a specific line height, see Phillipe's answer).Long answer: In the print and online world, the space between lines of text is known as "leading" (rhymes with 'heading', and comes from the lead metal used decades ago). Leading is a read-only property of
UIFont
, which was deprecated in 4.0 and replaced bylineHeight
. As far as I know, there's no way to create a font with a specific set of parameters such aslineHeight
; you get the system fonts and any custom font you add, but can't tweak them once installed.There is no spacing parameter in
UILabel
, either.I'm not particularly happy with
UILabel
's behavior as is, so I suggest writing your own subclass or using a 3rd-party library. That will make the behavior independent of your font choice and be the most reusable solution.I wish there was more flexibility in
UILabel
, and I'd be happy to be proven wrong!从 ios 6 开始,您可以在 UILabel 中设置属性字符串:
Starting in ios 6 you can set an attributed string in the UILabel:
您可以控制故事板中的行间距:
重复问题
You can control line spacing in the storyboard:
duplicate question
从 Interface Builder:
以编程方式:
SWift 4
使用标签扩展
现在调用扩展函数
或使用标签实例(只需复制并执行此代码即可查看结果)
Swift 3
From Interface Builder:
Programmatically:
SWift 4
Using label extension
Now call extension function
Or using label instance (Just copy & execute this code to see result)
Swift 3
我的解决方案是修补字体文件本身并确定其行高。
http://mbauman.net/geek/2009/03 /15/minor-truetype-font-editing-on-a-mac/
我必须修改“hhea”块中的“lineGap”、“ascender”、“descender”(如博客示例中所示)。
My solution was to patch the font file itself and fix its line height definitely.
http://mbauman.net/geek/2009/03/15/minor-truetype-font-editing-on-a-mac/
I had to modify 'lineGap', 'ascender', 'descender' in the 'hhea' block (as in the blog example).
这家伙创建了一个类来获取行高(不使用 CoreText,作为 MTLabel 库): https://github.com/LemonCake/MSLabel< /a>
This guy created a class to get line-height (without using CoreText, as MTLabel library) : https://github.com/LemonCake/MSLabel
我发现的最好的事情是: https://github.com/mattt/TTTAttributedLabel
这是一个 UILabel 子类,所以你可以将其放入,然后更改行高:
Best thing I found is: https://github.com/mattt/TTTAttributedLabel
It's a UILabel subclass so you can just drop it in, and then to change the line height:
我找到了像这样的第 3 方库:
https://github.com/Tuszy/MTLabel
是最简单的解决方案。
I've found 3rd Party Libraries Like this one:
https://github.com/Tuszy/MTLabel
To be the easiest solution.
这是一些快速代码,供您以编程方式设置行距
Here's some swift-code for you to set the line spacing programmatically
当然,如果您以编程方式传递字符串,迈克的答案将不起作用。在这种情况下,您需要传递属性字符串并更改其样式。
Of course, Mike's answer doesn't work if you pass the string programmatically. In this case you need to pass a attributed string and change it's style.