Search code examples
iostextcore-graphics

How to right align text using Core Graphics


I'd like to right align several text when drawing using Core Graphics. Below is the code I'm using to draw text now. How can I draw several texts that are right aligned?

CGContextSelectFont(context, "Helvetica-Light", 10.0f,kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetFillColorWithColor(context, _privateColor.CGColor);
CGContextShowTextAtPoint(context, point.x, point.y, [text cStringUsingEncoding:NSUTF8StringEncoding], text.length);

Solution

  • perhaps you're looking for something like this...

    [@"Any text what you like to show" drawInRect:CGRectMake(0.f, 0.f, 320.f, 80.f) withFont:[UIFont fontWithName:@"Helvetica-Light" size:10.f] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];
    

    update

    that way is unfortunately deprecated in iOS7+. source.