iosobjective-cnsattributedstringcore-textnsparagraphstyle

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


I have a NSAttributedString which is a "table" using NSTextTab stops where the first column is the KEY and the second column is the value

Like this:

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

The font for the KEYS is different size/weight + it is all caps. The rows align so that the bottoms of all the letters match up. I would like to nudge the values up vertically so that they appear to be vertically centered with the KEYS.

Is there any way to do this? This seems highly specialized, but from a design perspective I think it will look much better


Solution

  • I haven't tried it, but it seems like you should be able to use NSBaselineOffsetAttributeName for this. Here's what the docs say about it:

    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.

    So you'll probably have to compute the number of points needed to move the values up -- I guess that'd be half the difference in font sizes between the keys and values. Then apply NSBaselineOffsetAttributeName with that number of points to the text for the values.