Core Text trouble

Discussion in 'Mac Programming' started by Bunkum, May 28, 2011.

  1. macrumors newbie

    Jun 4, 2010
    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?
  2. jiminaus, May 28, 2011
    Last edited: May 28, 2011

    macrumors 65816


    Dec 16, 2010
    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.
  3. thread starter macrumors newbie

    Jun 4, 2010
    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?
  4. Moderator emeritus


    Jun 15, 2000
    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];

Share This Page