Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone center text ShowTextAtPoint

Tags:

iphone

I am looking for a way to center text on the iPhone using the context.ShowTextAtPoint() method.

like image 995
Ian Vink Avatar asked Dec 30 '22 04:12

Ian Vink


1 Answers

Beter late than never:

First put the text on the view (in invisible mode):

CGTextDrawingMode mode = CGContextGetTextDrawingMode(ctx);
CGContextSetTextDrawingMode(ctx, kCGTextInvisible);
CGContextShowTextAtPoint(ctx, 0, 0, @"test", strlen("test"));

Then get the position of the text and set the mode back to visible:

CGPoint pt = CGContextGetTextPosition(ctx);
CGContextSetTextDrawingMode(ctx, mode);

Now you have the position of the invisible text. Then use the center of the screen (160) and put a new text on it.

CGContextShowTextAtPoint(ctx, 160 - pt.x / 2, 200, @"test", strlen("test"));
like image 171
Ben Groot Avatar answered Jan 05 '23 00:01

Ben Groot