使文字在核心图形中上下颠倒 [英] getting the text up-side down in core graphic

查看:100
本文介绍了使文字在核心图形中上下颠倒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下方式在核心图形中绘制文本

 -(void)drawRect:(CGRect)rect {
CGContextTranslateCTM(context,20,150);
CGContextScaleCTM(context,1,1);

//使用MyDrawText函数绘制文本
myDrawText(context,viewBounds);

}
void myDrawText(CGContextRef myContext,CGRect contextRect)
{
CGFloat w,h;
w = contextRect.size.width;
h = contextRect.size.height;

CGAffineTransform myTextTransform;
CGContextSelectFont(myContext,
Helvetica-Bold,
h / 12,
kCGEncodingMacRoman);
CGContextSetCharacterSpacing(myContext,.5);
CGContextSetTextDrawingMode(myContext,kCGTextFill);

CGContextSetRGBFillColor(myContext,1,1,1,1);
myTextTransform = CGAffineTransformMakeRotation(0);
CGContextSetTextMatrix(myContext,myTextTransform);
CGContextShowTextAtPoint(myContext,115,0,成功,10);
}

运行后,我得到的文字上下颠倒,如下所示



为什么

请在此问题上向我提出建议。

解决方案

这是翻转它的方法。只需在drawRect中添加2行

 -(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextTranslateCTM(context,20,150);
CGContextScaleCTM(context,1,1);
CGAffineTransform flipVertical = CGAffineTransformMake(1,0,0,-1,0,0);
CGContextConcatCTM(context,flipVertical);

//使用MyDrawText函数绘制文本
myDrawText(context,self.bounds);

}


I am drawing a text in core graphic by following

- (void)drawRect:(CGRect)rect {
    CGContextTranslateCTM(context, 20, 150);
    CGContextScaleCTM(context, 1, 1);

    // Draw the text using the MyDrawText function
    myDrawText(context, viewBounds);

}
void myDrawText (CGContextRef myContext, CGRect contextRect) 
{
    CGFloat w, h;
    w = contextRect.size.width;
    h = contextRect.size.height;

    CGAffineTransform myTextTransform;
    CGContextSelectFont (myContext, 
                         "Helvetica-Bold",
                         h/12,
                         kCGEncodingMacRoman);
    CGContextSetCharacterSpacing (myContext, .5); 
    CGContextSetTextDrawingMode (myContext, kCGTextFill); 

    CGContextSetRGBFillColor (myContext, 1, 1, 1, 1); 
    myTextTransform =  CGAffineTransformMakeRotation  (0); 
    CGContextSetTextMatrix (myContext, myTextTransform); 
    CGContextShowTextAtPoint (myContext, 115, 0, "Successful", 10); 
}

After running it I am getting the text is in up-side down like below

Why the text is up side down after the drawing

Please advice me on this issue.

解决方案

this is the way to flip it. Just add 2 lines to your drawRect

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 20, 150);
CGContextScaleCTM(context, 1, 1);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, 0);
CGContextConcatCTM(context, flipVertical);

// Draw the text using the MyDrawText function
myDrawText(context, self.bounds);

}

这篇关于使文字在核心图形中上下颠倒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆