gpt4 book ai didi

iPhone PDF 创建文本反转问题

转载 作者:行者123 更新时间:2023-11-29 04:46:47 25 4
gpt4 key购买 nike

我正在创建一个用于创建 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, 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);
}

当我使用这段代码时,我得到的输出是这样的,第一个和第三个单词很好,但第二个单词就像倒置的(头朝下)。为什么这样?有什么想法吗?

最佳答案

问题出在 CGContextRef theContext = UIGraphicsGetCurrentContext(); 上,当每次函数调用时,CGContext 都会采用最后的状态,这就是获取反转输出的原因。所以,我补充说,

CGContextRef theContext = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(theContext);
//code to draw....
CGContextRestoreGState(theContext);

这解决了这个问题。谢谢:)

关于iPhone PDF 创建文本反转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9515049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com