CGContextShowText is rendering upside down text (mirror image)

你。 提交于 2020-01-02 05:36:06

问题


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.


回答1:


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);



回答2:


Swift 3

    let context = UIGraphicsGetCurrentContext()!
    let textTransform = CGAffineTransform(scaleX: 1.0, y: -1.0)
    context.textMatrix = textTransform



回答3:


The best solution to this problem is to simply call this at the beginning of your draw routine. (Make sure to only call it once.)

    CGAffineTransform transform;
    transform = CGAffineTransformConcat(CGContextGetTextMatrix(ctx),
                                        CGAffineTransformMake(1.0, 0.0, 0.0,
                                                              -1.0, 0.0, 0.0));
    CGContextSetTextMatrix(ctx, transform);


来源:https://stackoverflow.com/questions/7213533/cgcontextshowtext-is-rendering-upside-down-text-mirror-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!