PDA

View Full Version : Core Text trouble




Bunkum
May 28, 2011, 02:02 AM
At the moment I'm simply drawing my text of one font and size in a CTFrame. However I want to be able to have multiple different font and sized texts in the same CTFrame or whatever way would let me achieve the same effect.


I thought about making a CTRun for each type but I'm not sure how I can go about drawing them so it's like if I was to draw from a CTFrame.

Or a CTFrame for each type and just restructuring the path for each, but problem with this is even though I can get the end x and y points from a CTFrame I don't think I can get the height of individual glyphs. So I won't know how much to restructure the path by.

So how do I go about drawing multiple texts with different attributes?



jiminaus
May 28, 2011, 04:57 AM
Set up an attributed string to contain the text and formatting on the text. You can have different attributes applied to different subranges of the string.

Use CTFramesetter to create CTFrames. CTFramesetter will use a CTTypesetter to lay out the lines and runs in the frame.

See the Core Text Programming Guide (http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming). Particularly the Common Operations chapter has example code. See the CoreTextPDF (http://developer.apple.com/library/mac/#samplecode/CoreTextRTF) sample for a working example project.

But is there good reason for using CoreText directly? Why not use the higher-level Cocoa text system? For example, by using NSAttributedString's drawWithRect:options: method.

Bunkum
May 28, 2011, 09:42 AM
Didn't realize that you could set attributes for only certain ranges. So everything should be fine I think when I change to doing that. Thanks.

And my reason for using Core Text is because I want to be able to make use of the text in OpenGL. So I'm currently drawing to a CGBitmapContext then copying that to a GL texture. I think I could use drawWithRect:options: but I would have to create a view and then get the image from that, I couldn't just simply make it draw to the bitmap context?

kainjow
May 28, 2011, 04:17 PM
Something like this should work for using Cocoa methods to draw into your CGBitmapContext:

CGContextRef ctx = CGBitmapContextCreate(...);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO]];
// do your drawing
[NSGraphicsContext restoreGraphicsState];