user2608857 user2608857 - 1 year ago 135
iOS Question

Center two different size fonts vertically in an NSAttributedString

I use NSAttributedString to generate a string with two different sizes. But by default its bottom alignment looks like this:

baseline aligned sizes

But I want to center it vertically, like this:
vertically centered sizes

To be clear, this is a single attributed string, not two or more. This is a simplified example to describe my question, what I'd actually like to do is more complex.

Answer Source

I'd say the easiest thing to do is just manipulate the NSBaselineOffsetAttributeName attribute for the text in question:


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.

To center, you'd take the difference between height of the large text and the height of the smaller text and halve it, then use that as the baseline adjustment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download