使用Objective-C /核心图形的水平中心文字 [英] Horizontal center text using objective-c / core graphics

查看:95
本文介绍了使用Objective-C /核心图形的水平中心文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Core Graphics在iPhone屏幕上居中放置文本,并且发现

I'm trying to center a text on an iPhone screen using Core Graphics and I found this code.

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
NSString * text = @"text";
CGFloat minHeight = 40;
float widthIs = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, minHeight) options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:22.0] }
context:nil].size.width;
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor);
[text drawAtPoint:CGPointMake((screenWidth - widthIs)/2, (screenHeight - minHeight)/2) withFont: [UIFont systemFontOfSize:22.0]];

但是我自己尝试使用此代码无法真正起作用。文本不是真正地集中在屏幕上,我也不知道如何编辑代码来解决我的问题。

But trying this code for my own I can not really get it working. The text is not really centered on my screen and I have no idea how to edit the code to solve my problem.

这是我得到的结果:

推荐答案

其他人指出您的代码基本上是正确的,为什么它不显示您期望的文本?

Others have stated that your code is basically correct, so why is it not displaying the text where you expect?

我的猜测是您没有在自己认为和/或对坐标系感到困惑的视图中绘制图形。

My guess is that you are not drawing in the view you think you are and/or are confused over coordinate systems.

使用当前视图的边界而不是屏幕将使您的文本在该视图中居中,如果视图本身在屏幕上居中,则很好,但如果不在屏幕上,则也将显示在中心之外。

Using the current view's bounds instead of the screen will centre your text in that view, which is fine if the view itself is centered on the screen but would also display off centre if not.

您应该弄清楚您实际上是在哪个坐标系中绘制和/或读取的视图。

You should figure out what view you are actually drawing in and/or read up on coordinate systems.

要将文本绘制在坐标系的中心屏幕,无论视图如何,都可以像在屏幕坐标,然后将其转换为视图的坐标并在此处绘制。例如。如果您的代码在视图的 drawRect 内部,则类似以下内容:

To draw the text in the centre of the screen regardless of the view you can calculate the origin as you have in screen coordinates and then convert it to the view's coordinates and draw there. E.g. if your code is inside a view's drawRect then something like:

// Save the point in screen coordinates 
CGPoint onScreenOrigin = CGPointMake((screenWidth - widthIs)/2, (screenHeight - minHeight)/2);
// Convert from screen to current view's coordinates
CGPoint inViewOrigin = [self convertPoint:onScreenOrigin fromView:nil];
// Draw the text
[text drawAtPoint:inViewOrigin ...];

无论视图的大小和位置如何,这都会在屏幕中央绘制文本-当然,只要视图覆盖了需要文本的屏幕区域,否则文本将被裁剪到视图中并且可能不完整或完全不可见!

This will draw the text in the centre of the screen regardless of the size and position of the view - provided of course that the view covers the area of the screen where the text needs to be, if it doesn't the text will be cropped to the view and may incomplete or completely invisible!

HTH

这篇关于使用Objective-C /核心图形的水平中心文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆