如何在具有不同字体的 NSAttributedString 中垂直居中一行?

How can I vertically center a line in an NSAttributedString with different fonts?

我有一个 NSAttributedString,它是一个 "table" 使用 NSTextTab 停止,其中第一列是 KEY,第二列是值

像这样:

|  **KEY 1**|value1|
|**KEY TWO**|value2|

KEYS 的字体不同 size/weight + 全部大写。 行对齐,以便所有字母的底部匹配。我想将值垂直向上移动,使它们看起来与 KEYS 垂直居中。

有什么办法吗?这看起来很专业,但从设计的角度我认为它会更好看

我还没有尝试过,但看起来您应该可以为此使用 NSBaselineOffsetAttributeName。文档是这样说的:

The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 0.

所以您可能必须计算将值向上移动所需的点数——我猜这将是键和值之间字体大小差异的一半。然后将具有该点数的 NSBaselineOffsetAttributeName 应用于值的文本。