acb's technical journal

Posts matching tags 'catextlayer'


Automatically sizing CATextLayer to fit

When developing on iOS, I am a big fan of CoreAnimation, and particularly the CALayer and its subclasses; they're lighter in weight than UIView, carrying less baggage, and are a quick way to display all sorts of elements in a user interface.

CALayer has a number of useful subclasses, among them CATextLayer, which displays text in an arbitrary font and size* within its bounds. One shortcoming of CATextLayer, however, is that it does not provide any means of measuring the dimensions of its contents, requiring you to guesstimate them and size the layer to fit, or else pre-measure the string before sizing your layer. This is complicated somewhat by the fact that CATextLayer can accept font settings in several forms (string font name, CoreText CTFont or CoreGraphics CGFont), none of which are the convenient UIFont object.

Having had reason to create a precisely-sized CATextLayer recently, I looked into the issue and have written a small category extending CATextLayer, adding a method for setting its bounds to neatly encompass its text.


calayer catextlayer cocoa coreanimation coretext ios objective c 0

This will be the comment popup.
Post a reply
Display name:

Your comment:

Please enter the text in the image above here: