核心文本 - NSAttributedString 行高做对了吗? [英] Core Text - NSAttributedString line height done right?

查看:57
本文介绍了核心文本 - NSAttributedString 行高做对了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Core Text 的行间距完全一无所知.我正在使用 NSAttributedString 并在其上指定以下属性:- kCTFontAttributeName- kCTParagraphStyleAttributeName

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

由此创建 CTFrameSetter 并将其绘制到上下文中.

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.

当我使用 kCTParagraphStyleSpecifierLineHeightMultiple 时,每行在文本顶部接收填充,而不是在此高度的中间显示文本.

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.

当我使用 kCTParagraphStyleSpecifierLineSpacing 时,文本底部会添加一个填充.

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.

如果不走明确创建 CTLine 等的路线,这是不可能的吗?

推荐答案

我仍然不是 100% 相信我的以下陈述,但它似乎有道理.请纠正我的错误.

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:

// NOT SURE WHAT THE THEORY BEHIND THIS FACTOR IS. WAS FOUND VIA TRIAL AND ERROR.
    CGFloat factor = 14.5/30.5;
    CGFloat floatValues[4];
    floatValues[0] = self.lineHeight * factor/(factor + 1);
    floatValues[1] = self.lineHeight/(factor + 1);
    floatValues[2] = self.lineHeight;

此矩阵与 NSAttributedString 的段落样式参数一起使用:

This matrix is used with the paragraph style parameter for NSAttributedString:

CTParagraphStyleSetting paragraphStyle[3];

paragraphStyle[0].spec = kCTParagraphStyleSpecifierLineSpacing;
paragraphStyle[0].valueSize = sizeof(CGFloat);
paragraphStyle[0].value = &floatValues[0];

paragraphStyle[1].spec = kCTParagraphStyleSpecifierMinimumLineHeight;
paragraphStyle[1].valueSize = sizeof(CGFloat);
paragraphStyle[1].value = &floatValues[1];

paragraphStyle[2].spec = kCTParagraphStyleSpecifierMaximumLineHeight;
paragraphStyle[2].valueSize = sizeof(CGFloat);
paragraphStyle[2].value = &floatValues[2];

CTParagraphStyleRef style = CTParagraphStyleCreate((const CTParagraphStyleSetting*) &paragraphStyle, 3);
[attributedString addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0, [string length])];
CFRelease(style);

希望这对某人有所帮助.我会在发现更多相关信息时更新此答案.

Hope this helps someone. I'll update this answer as I discover more relevant information.

这篇关于核心文本 - NSAttributedString 行高做对了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆