简体   繁体   English

使用Objective-C /核心图形的水平中心文本

[英]Horizontal center text using objective-c / core graphics

I'm trying to center a text on an iPhone screen using Core Graphics and I found this code . 我正在尝试使用Core Graphics在iPhone屏幕上居中放置文本,并找到了这段代码

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. 文本不是真正地集中在屏幕上,我也不知道如何编辑代码来解决我的问题。

This is the result I get: 这是我得到的结果:

图片

This looks basically right: A few minor observations: 这看起来基本上是正确的:一些小发现:

  1. Rather than referring to the UIScreen , I'd just refer to the view in which the text was rendered. 除了引用UIScreen ,我只引用呈现文本的视图。 Eg if actually in some UIView subclass implementation, you'd just refer to self.bounds.size . 例如,如果实际上在某些UIView子类实现中, self.bounds.size引用self.bounds.size

  2. I would use the attributes rendition for not only getting the size of the bounding rect, but for drawAtPoint , too. 我不仅可以使用attributes rendition来获取边界rect的大小,还可以使用drawAtPoint Eg Use drawAtPoint:withAttributes: instead of drawAtPoint:withFont: . 例如,使用drawAtPoint:withAttributes:而不是drawAtPoint:withFont:

  3. The font color should be part of the attributes dictionary, too. 字体颜色也应该是attributes字典的一部分。

For example: 例如:

CGSize viewSize = self.bounds.size;
NSString *text = @"text";
CGFloat minHeight = 40;
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:22.0], NSForegroundColorAttributeName: [UIColor redColor]};
CGSize textSize = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, minHeight)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil].size;
[text drawAtPoint:CGPointMake((viewSize.width - textSize.width)/2, (viewSize.height - textSize.height)/2) withAttributes:attributes];

If it's still not centered after you do this, you'd want to confirm the frame of the UIView in which you are rendering this, and make sure the it is where you think it should be. 如果执行此操作后仍未居中,则需要确认要在其中呈现此内容的UIViewframe ,并确保该frame位于您认为应该的位置。 You can run the app in the debugger and then use the view debugger 您可以在调试器中运行应用程序,然后使用视图调试器 查看调试器 to confirm where the view is located. 确认视图所在的位置。

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. 您应该弄清楚您实际绘制的视图和/或在坐标系上阅读的视图。

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. 要在屏幕中央绘制文本,而不管视图如何,都可以像在屏幕坐标中那样计算原点,然后将其转换为视图的坐标并在此处绘制。 Eg if your code is inside a view's drawRect then something like: 例如,如果您的代码在视图的drawRect则类似于:

// 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 HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM