Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
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
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.:mad:
 
I found out doing likw this
Is working more properly
Code:
if(rotation != 0) {
        CGContextTranslateCTM( context, locX + (charactersWidth/2), locY + (charactersHeight / 2) );
        CGContextRotateCTM( context, M_PI / 4);
        CGContextTranslateCTM( context, (locX +(charactwrsWidth / 2) * -1), (locY + (charactersWidth /2) * -1) );
    }

Which will rotate the current context 45 degrees Clockwise

So my mind goes like this
Get center of the context
Rotate by pi / 4. Why the heck? I dont know why
Then translate the whole position back to original in negative, since the first call ********d the position up prety much

I dont understand the pi / 4
I think its like.. Devide circel in 4 pieces
So I could do like ((M_PI / 360) * rotation) ??

And will rotion = 0 keep it in original angle?

Will this bring me to the result Im trying to achieve?
(Im not at work now and I dont have an mac and xcode for myself) yet...

If anyone has an better solution or maybe function for me, Id like to hear/see

Also strugling around with the dynamic context rectangle, since its not quite accurate in width :p
And how to put in an UIColor als parameter I cant fix
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.