Hello all,
I been working on my application, and presentated to my chief and he had some little comments and things he'd like to see and how he wanted it to see.
The thing I'm struggling with is rotating text for the PDF I'm generating.
I wrote some functions to make life easier and am using this for my text
Proberbly there are few more things that could be done better besides the rotation...
Anyways, the rotation part is not working for me, when I'm doing any of these, it just disapears!
So after some googling I found out I first have to point out to the center of the context, then rotate and move it back???
I tried and tried and tried and tried, but I lost in my own thoughts.
Hopefully somebody can help me out here and explain me how I should be doing it.
I been working on my application, and presentated to my chief and he had some little comments and things he'd like to see and how he wanted it to see.
The thing I'm struggling with is rotating text for the PDF I'm generating.
I wrote some functions to make life easier and am using this for my text
Code:
-(void) drawText:(NSString*)theText locX:(float)locX locY:(float)locY fontSize:(float)fontSize alignment:(NSString *)alignment rotation:(float)rotation {
CGContextRef context = UIGraphicsGetCurrentContext();
UIFont *font = [UIFont fontWithName: @"Verdana" size: fontSize];
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
NSArray *resultLines = [theText componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"\n"]];
float charactersWidth = 0;
float charactersHeight = 0;
if(resultLines.count > 1) {
for (NSString *curLine in resultLines) {
if((curLine.length * fontSize) > charactersWidth) {
charactersWidth = curLine.length * fontSize;
}
}
charactersHeight = (resultLines.count + 1) * fontSize + 3;
} else {
charactersWidth = theText.length * fontSize;
charactersHeight = fontSize + 3;
}
if([alignment isEqual: @"center"]) {
locX = (pagesize.width / 2) - (charactersWidth / 2);
}
if(rotation != 0) {
CGContextTranslateCTM( context, locX + (charactersWidth/2), locY + (charactersHeight / 2) );
CGContextRotateCTM( context, 90 );
CGContextTranslateCTM( context, locX, locY );
// CGContextRotateCTM(context, 45);
}
CGRect textRec = CGRectMake(locX, locY, charactersWidth, charactersHeight);
NSString *myString = theText;
if([alignment isEqual: @"center"]) {
[myString drawInRect: textRec withFont: font lineBreakMode:NSLineBreakByWordWrapping alignment: NSTextAlignmentCenter];
} else if([alignment isEqual: @"right"]) {
[myString drawInRect: textRec withFont: font lineBreakMode:NSLineBreakByWordWrapping alignment: NSTextAlignmentRight];
} else {
[myString drawInRect: textRec withFont: font lineBreakMode:NSLineBreakByWordWrapping alignment: NSTextAlignmentLeft];
}
}
Proberbly there are few more things that could be done better besides the rotation...
Anyways, the rotation part is not working for me, when I'm doing any of these, it just disapears!
So after some googling I found out I first have to point out to the center of the context, then rotate and move it back???
I tried and tried and tried and tried, but I lost in my own thoughts.
Hopefully somebody can help me out here and explain me how I should be doing it.