No CoreText in watchOS 2

(Reposting in these forums since watchOS 2 is now GM.)


With WatchKit 1.x I used CoreText to render text into a CGContext (to place labels on graphs). As of watchOS 2, CoreText no longer appears to be available. I filed a radar (22596950) but in the mean time, what's the recommended way to render text into a CGContext on the watch?

You can use NSAttributedString + drawInRect.

That's awesome. I had no idea you could use the extended NSAttributedString drawing methods in watchOS.


FWIW anyone doing this with in a CG context with normal orientation, you'll need to flip the drawing port first. E.g.:


CGContextTranslateCTM(context, 0, pointSize);

CGContextScaleCTM(context, 1.0, -1.0);

UIFont *font = [UIFont systemFontOfSize:7 weight:UIFontWeightSemibold];

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];

[attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, text.length)];

[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:0.75 alpha:1.0] range:NSMakeRange(0, text.length)];

[attrStr drawAtPoint:CGPointMake(x, 0.0)];

CGContextScaleCTM(context, 1.0, -1.0);

CGContextTranslateCTM(context, 0, -pointSize);

No CoreText in watchOS 2
 
 
Q