Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextShowText is rendering upside down text (mirror image)

Without my doing anything to request upside down text, CGContextShowText is drawing it in that fashion. It is a mirror image vertically of what it should be.

float font_size = 19.;
CGContextSelectFont (context, "Helvetica", font_size, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (context, kCGTextFill); 
CGContextSetRGBStrokeColor (context, 255, 0, 0, 0); 
CGContextSetRGBFillColor (context, 255, 0, 0, 0); 
CGContextSetTextPosition (context, x, y);
CGContextShowText (context, cstring, strlen(cstring));

How can I fix this? Also why is this the default drawing mode?

Thanks.

like image 518
Nemo Avatar asked Aug 27 '11 09:08

Nemo


2 Answers

This "upsidedownness" commonly comes into play when the API renders to your cgContext from top to bottom, but somehow you're drawing from bottom to top. Anyway, the 2 line solution I use is:

CGAffineTransform trans = CGAffineTransformMakeScale(1, -1);
CGContextSetTextMatrix(tex->cgContext, trans);
like image 185
bobobobo Avatar answered Sep 27 '22 22:09

bobobobo


Swift 3

    let context = UIGraphicsGetCurrentContext()!
    let textTransform = CGAffineTransform(scaleX: 1.0, y: -1.0)
    context.textMatrix = textTransform
like image 33
MoFlo Avatar answered Sep 28 '22 00:09

MoFlo