iosuilabelcore-texttypographynsattributedstring

Drop cap with NSAttributedString


I would like to do a drop cap first character in a UILabel using the attributedText NSAttributedString property only. Like this:


(source: interpretationbydesign.com)

I have experimented with adjusting the base line for the range of the first character to a negative value, and it works for aligning the top of the first char with the top of the rest of the first line. But I have not found any way to make the other lines flow to the right of the drop capped character.

Can this be solved using NSAttributedString only, or do I have to split the string and render it myself using Core Text?


Solution

  • CoreText cannot do drop caps because it consists of lines made up of glyph runs. A drop cap would cover multiple lines which is not supported.

    To achieve this effect you would have to draw the cap separately and then draw the rest of the text in a path that goes around it.

    Long story short: not possible in UILabel, possible, but a fair bit of work with CoreText.

    The steps to do it with CoreText are: