Center two fonts with different different sizes vertically in an NSAttributedString

后端 未结 4 2011
一生所求
一生所求 2020-12-24 10:13

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

4条回答
  •  无人及你
    2020-12-24 10:56

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

    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.

    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.

提交回复
热议问题