Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
I'm trying to draw text to a custom UIView, but from what I can tell using Quartz 2D isn't quite as flexible as say, ATSU. For example, this text doesn't break at the newline character:

CGContextShowTextAtPoint(ctx, 10, 10, "this\nis a test", strlen("this\nis a test"));

and I don't think I can use bounding rects to have a body of text break naturally at the specified bounds.

What can i use alternatively? A lot of the documentation references ATSU, but doesn't provide links, which I'm guessing is Apple's subtle way of saying it's deprecated. I don't need anything as advanced as rtf, just something beyond what's available out of the box with CGContext
 
thanks for the responses. NSString won't work as i need to write to a CGContext. Sorry, just realized my original post didn't indicate that.

are there any other options? also is ATSU definitely deprecated?
 
In that case, you can still use Cocoa Text. You'll have to set the current graphics context to your CGContext temporarily, with something like this:

Code:
NSGraphicsContext *current = [NSGraphicsContext currentContext]; // for restoration later on
NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:yourCGContextRef flipped:NO];
[NSGraphicsContext setCurrentContext:nsContext];

// your Cocoa drawing here
// [myNSString drawInRect ...];

[NSGraphicsContext setCurrentContext:current];
 
NSGraphicsContext is part of AppKit which is not available on the iPhone.

If you're using CGContextRef directly you can use the NSString drawing methods. But you probably need to use UIGraphicsPushContext() and UIGraphicsPopContext() before and after you call any UIKit drawing methods.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.