iPhone中心文字ShowTextAtPoint

Ian*_*ink 4 iphone

我正在寻找一种使用context.ShowTextAtPoint()方法在iPhone上居中文本的方法.

Ben*_*oot 8

比从来没有迟到了:

首先将文本放在视图上(以隐形模式):

CGTextDrawingMode mode = CGContextGetTextDrawingMode(ctx);
CGContextSetTextDrawingMode(ctx, kCGTextInvisible);
CGContextShowTextAtPoint(ctx, 0, 0, @"test", strlen("test"));
Run Code Online (Sandbox Code Playgroud)

然后获取文本的位置并将模式设置回可见:

CGPoint pt = CGContextGetTextPosition(ctx);
CGContextSetTextDrawingMode(ctx, mode);
Run Code Online (Sandbox Code Playgroud)

现在你拥有了不可见文本的位置.然后使用屏幕中心(160)并在其上放置一个新文本.

CGContextShowTextAtPoint(ctx, 160 - pt.x / 2, 200, @"test", strlen("test"));
Run Code Online (Sandbox Code Playgroud)