核心文本 - NSAttributedString 行高做得对吗?
我对 Core Text 的行距一无所知。我正在使用 NSAttributedString 并在其上指定以下属性: - kCTFont属性名称 - kCTParagraphStyleAttributeName
由此创建 CTFrameSetter 并将其绘制到上下文中。
在段落样式属性中,我想指定行的高度。
当我使用 kCTParagraphStyleSpecifierLineHeightMultiple 时,每行都会在文本顶部接收填充,而不是在该高度的中间显示文本。
当我使用 kCTParagraphStyleSpecifierLineSpacing 时,文本底部会添加填充。
请帮助我实现指定的行高,其中文本(字形)位于该高度的中间,而不是文本位于行的底部或顶部。
如果不显式创建 CTLine 等,这是否不可能?
I'm completely in the dark with Core Text's line spacing. I'm using NSAttributedString and I specify the following attributes on it:
- kCTFontAttributeName
- kCTParagraphStyleAttributeName
From this the CTFrameSetter gets created and drawn to context.
In the paragraph style attribute I'd like to specify the height of the lines.
When I use kCTParagraphStyleSpecifierLineHeightMultiple each line receives padding at the top of the text, instead of the text being displayed in the middle of this height.
When I use kCTParagraphStyleSpecifierLineSpacing a padding is added to the bottom of the text.
Please help me achieve a specified line height with the text(glyphs) in the middle of that height, instead of the text sitting either at the bottom or the top of the line.
Is this not possible without going down the route of explicitly creating CTLine 's and so forth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以从故事板以及以编程方式设置/更新行距和行高倍数。
从 Interface Builder:
以编程方式:
SWift 4
现在调用扩展函数
或使用标签实例(只需复制并执行此代码即可查看结果)
Swift 3
You can set/update line spacing and line height multiple from storyboard as well as programatically.
From Interface Builder:
Programmatically:
SWift 4
Now call extension function
Or using label instance (Just copy & execute this code to see result)
Swift 3
我尝试了所有这些答案,但要真正获得 Sketch 或 Zeplin 设计文件中通常出现的精确线高,那么您需要:
I tried all these answers, but to really get the EXACT line height that usually comes in design files from Sketch or Zeplin then you need to:
我为此做了一个扩展,见下文。使用扩展,您可以像这样设置行高:
这是扩展:
注意:
I made an extension for this, see below. With the extension you can just set the line height like so:
This is the extension:
Notes:
NSParagraphStyle
有两个属性可以修改同一段落中的连续文本基线之间的高度:lineSpacing
和lineHeightMultiple
。 @Schoob 是对的,1.0
以上的lineHeightMultiple
在文本上方添加了额外的空间,而0.0
以上的lineSpacing
添加了文本下方的空间。 此图显示了各个维度之间的关系。因此,为了使文本保持居中,目的是根据另一个属性来指定一个属性,这样我们通过一个属性(顶部/底部)添加的任何“填充”都可以通过确定另一个属性的填充(底部/底部)来平衡。顶部)来匹配。换句话说,添加的任何额外空间都会均匀分布,同时保留文本的现有位置。
好处是,通过这种方式,您可以选择要指定的属性,然后确定另一个:
从这里开始,其他答案显示了如何在
NSAttributedString
中设置这两个属性,但这应该回答两者如何与“居中”文本相关。There are two properties of
NSParagraphStyle
that modify the height between successive text baselines in the same paragraph:lineSpacing
andlineHeightMultiple
. @Schoob is right that alineHeightMultiple
above1.0
adds additional space above the text, while alineSpacing
above0.0
adds space below the text. This diagram shows how the various dimensions are related.To get the text to stay centred the aim is therefore to specify one in terms of the other, in such a way that any 'padding' we add by one attribute (top/bottom) is balanced by determining the other attribute's padding (bottom/top) to match. In other words, any extra space added is distributed evenly while otherwise preserving the text's existing positioning.
The nice thing is that this way you can choose which attribute you want to specify and then just determine the other:
From here, other answers show how these two attributes can be set in an
NSAttributedString
, but this should answer how the two can be related to 'centre' the text.Swift 4 和5
Swift 4 & 5
这在 Xcode 7.2 中对我有用。 iOS 9.2.1。 (斯威夫特 2.1。):
This worked for me in Xcode 7.2. iOS 9.2.1. (Swift 2.1.):
使用 NSAttributedString 线位置进行扭动的另一种方法是玩
baselineOffset 属性:
结果:
“我明白了
我认为这既是通知又是
查看过去公告的地方
就像单向聊天一样。”
https://stackoverflow.com/a/55876401/4683601
Another way of twerking with a NSAttributedString line position is playing with
baselineOffset attribute:
Result:
"I see
I'd think it`d be both a notification and a
place to see past announcements
Like a one way chat."
https://stackoverflow.com/a/55876401/4683601
Objective-C
Swift 5
Objective-C
Swift 5
我对下面的说法仍然不是100%有信心,但似乎有道理。有不对的地方请指正。
行高(行距)是指连续行的基线之间的距离。这里的基线可以解释为文本所在的假想线。
行距是行与行之间的间距。空格出现在文本行之后。
我最终使用以下解决方案来解决我的问题:
此矩阵与 NSAttributedString 的段落样式参数一起使用:
希望这对某人有帮助。当我发现更多相关信息时,我会更新这个答案。
I'm still not 100% confident in my following statements, but it seems to make sense. Please correct me where I am wrong.
The line height (leading) refers to the distance between the baselines of successive lines of type. The baseline here can be interpreted as the imaginary line which the text sits on.
Spacing is the space between lines. The space appears after the line of text.
I ended up using the following solution to my problem:
This matrix is used with the paragraph style parameter for NSAttributedString:
Hope this helps someone. I'll update this answer as I discover more relevant information.
在 Swift 3 中:
In Swift 3: