i am creating an iphone app for creating pdf. I am using the following code:
[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);
}
When I use this code I am getting the output like, first and third word is fine but the second one is coming like inverted (head down). Why so? Any idea?
The issue was with CGContextRef theContext = UIGraphicsGetCurrentContext();
, when each time the function calls, the CGContext
taking the last state, that's why getting the inverting output. So, i added,
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(theContext);
//code to draw....
CGContextRestoreGState(theContext);
this solved the issue. Thanks :)