iPhone PDF创建文本反转问题
我正在创建一个用于创建pdf的iphone应用程序。我正在使用以下代码:iPhone PDF创建文本反转问题,iphone,ios,quartz-2d,Iphone,Ios,Quartz 2d,我正在创建一个用于创建pdf的iphone应用程序。我正在使用以下代码: [self drawRect:CGRectMake(0, 0, 100, 20 )text:@"last Name"xVal:10 yVal:-30 len:9]; [self drawRect:CGRectMake(0, 0, 100, 20 )text:@"First Name"xVal:180 yVal:30 len:10]; [self drawRect:CGRectMake(0, 0, 1
[self drawRect:CGRectMake(0, 0, 100, 20 )text:@"last Name"xVal:10 yVal:-30 len:9];
[self drawRect:CGRectMake(0, 0, 100, 20 )text:@"First Name"xVal:180 yVal:30 len:10];
[self drawRect:CGRectMake(0, 0, 100, 20 )text:@"Middle Name"xVal:330 yVal:-30 len:11];
+ (void) drawRect:(CGRect)rect text:(NSString *)string xVal:(int)x yVal:(int)y len:(int)length{
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGRect viewBounds = rect;
CGContextTranslateCTM(theContext, 0, viewBounds.size.height);
CGContextScaleCTM(theContext, 1, -1);
// Draw the text using the MyDrawText function
MyDrawText(theContext, viewBounds,string, x, y,length);
}
void MyDrawText (CGContextRef myContext, CGRect contextRect, NSString *textToDraw, int x, int y, int len){
float w, h;
w = contextRect.size.width;
h = contextRect.size.height;
CGAffineTransform myTextTransform;
CGContextSelectFont (myContext,"Helvetica-Bold", h, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (myContext, 3);
CGContextSetTextDrawingMode (myContext, kCGTextFill);
CGContextSetRGBFillColor(myContext, 0, 0, 0, 1);
CGContextSetRGBStrokeColor (myContext, 0, 0, 0, 0);
myTextTransform = CGAffineTransformMakeRotation(0.0);
CGContextSetTextMatrix (myContext, myTextTransform);
const char *str = [textToDraw UTF8String];
CGContextShowTextAtPoint (myContext, x, y,str, len);
}
当我使用这段代码时,我得到的输出是,第一个和第三个单词是好的,但是第二个是倒转的(头朝下)。为什么会这样?有什么想法吗?
核心图形
将左下角作为绘图原点。(在UIKit
its左上角)
这里在方法调用中,为第二个字符串提供yVal
as 30和其他值-30
谢谢,问题在于
CGContextRef theContext=UIGraphicsGetCurrentContext()
,每次函数调用时,CGContext
将进入最后一个状态,这就是为什么会得到反向输出。所以我补充说,
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(theContext);
//code to draw....
CGContextRestoreGState(theContext);
这就解决了问题。谢谢:)yVal刚刚更改了Y轴上的位置。这不是控制文本的反转性质。我把它改成了30,仍然没有改变,但是位置已经改变了。