CoreText alternatives to deprecated CGContext text functions
Asked Answered
G

1

6

In iOS 7, several of the CGContext text drawing functions were deprecated in favor of Core Text:

Deprecated CGContext Functions

Sometimes Apple suggests specific alternatives to deprecated functions (e.g. "use x instead") but in this case, "x" is simply "Core Text", which isn't all that specific.

So, if I'm just in -drawRect: and I've got my CGContextRef and I'm ready to call CGContextShowGlyphsAtPoint(), what do I do instead? What's the best practice, and what's the simplest Core Text replacement that doesn't require refactoring any drawing code beyond that call (if such a thing exists)?

Bonus points for an answer that concisely answers this for each of the deprecated text functions on that page—I'm most interested in CGContextShowGlyphsAtPoint(), but this could serve as a good reference for others seeking alternatives to any of those functions.

Grisette answered 13/10, 2013 at 20:8 Comment(1)
You may give CTFontDrawGlyphs a try. You'll probably have to do some calculations, but this seems the best method that I've seen.Carman
K
2

I believe the intention is for CGContextShowGlyphsAtPoint to be replaced by CGContextShowGlyphsAtPositions. You can get the glyph positions with CTRunGetPositionsPtr or (if that returns NULL), CTRunGetPositions.

AFAICT, CGContextShowGlyphsAtPositions also replaces several other former CoreText functions, such as CGContextShowGlyphs, CGContextShowText, and CGContextShowTextAtPoint. Other former CoreText functions like CGContextSelectFont are replaced by multiple new functions (in that case, CGContextSetFont and CGContextSetFontSize). I believe this is all fairly self-explanatory by reading the API.

There may be other deprecated functions I didn't specifically mention, but I figure you get the point.

Kroll answered 29/7, 2015 at 0:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.