On the importance of setting contentsScale in CATextLayer

The retina display iPhone has been out for 2 years now, so I probably should have run into this before, but as I use CATextLayer infrequently I had not.  Anyway, if you don’t manually set the contentsScale property of your CATextLayer to the correct value, on retina devices you’ll end up with blocky pixel-doubled text like this:

However, if you call:

if ([layer respondsToSelector:@selector(setContentsScale:)])
    [layer setContentsScale:[[UIScreen mainScreen] scale]];

then you will get properly rendered text like so:


(Solution via StackOverflow.)

Author: Mark

Mark is an American computer programmer living in Switzerland.