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

1

5

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

Adrianaadriane answered 22/5, 2015 at 20:26 Comment(4)
You mean horizontally center if you want to align the bottom line.Adiabatic
@ThomasKilian No, vertically centered is correct. As I understand the question, the OP wants the Y-values of the centers of the key and its corresponding value to match.Saidee
@Saidee Yes. This is called a horizontal alignment. Where on a horizontal line you dock element on an imaginary line.Adiabatic
While I understand how you could interpret it both ways as it's an imaginary horizontal line, I believe that vertical alignment generally refers to lining up in the Y direction.Adrianaadriane
S
10

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.

Saidee answered 22/5, 2015 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.