Drawing Text with Core Graphics

后端 未结 3 1277
庸人自扰
庸人自扰 2021-02-02 02:15

I need to draw centered text to a CGContext.

I started with a Cocoa approach. I created a NSCell with the text and tried to draw it thus:

NSGraphicsConte         


        
3条回答
  •  孤城傲影
    2021-02-02 03:07

    I would continue with your above approach but use NSAttributedString instead.

    NSGraphicsContext* newCtx = [NSGraphicsContext graphicsContextWithGraphicsPort:bitmapContext flipped:true];
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:newCtx];
    NSAttributedString *string = /* make a string with all of the desired attributes */;
    [string drawInRect:locationToDraw];
    [NSGraphicsContext restoreGraphicsState];
    

提交回复
热议问题