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

Bunkum

macrumors newbie
Original poster
Jun 4, 2010
11
0
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

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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. Particularly the Common Operations chapter has example code. See the CoreTextPDF 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.
 
Last edited:

Bunkum

macrumors newbie
Original poster
Jun 4, 2010
11
0
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:eek:ptions: 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

Moderator emeritus
Jun 15, 2000
7,958
7
Something like this should work for using Cocoa methods to draw into your CGBitmapContext:

Code:
CGContextRef ctx = CGBitmapContextCreate(...);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:NO]];
// do your drawing
[NSGraphicsContext restoreGraphicsState];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.