【问题标题】:Horizontal center text using objective-c / core graphics使用objective-c / core graphics的水平居中文本
【发布时间】:2017-12-30 15:59:32
【问题描述】:

我正在尝试使用 Core Graphics 使文本在 iPhone 屏幕上居中,但我找到了 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]];

但是为我自己尝试这段代码我无法真正让它工作。文本并没有真正集中在我的屏幕上,我不知道如何编辑代码来解决我的问题。

这是我得到的结果:

【问题讨论】:

  • 您能用您的解决方案更新上面的代码吗?抱歉,我是目标等方面的新手……:/ @Rob
  • 当您说您在另一个 StackOverflow 问题中找到了答案时,请实际链接到该问题(或直接链接到答案)。
  • 更新@DavidStockinger

标签: ios objective-c core-graphics


【解决方案1】:

这看起来基本正确:一些小的观察:

  1. 与其引用UIScreen,我只想引用呈现文本的视图。例如。如果实际上在某些UIView 子类实现中,您只需参考self.bounds.size

  2. 我将使用attributes 再现不仅获取边界矩形的大小,还获取drawAtPoint。例如。使用drawAtPoint:withAttributes: 而不是drawAtPoint:withFont:

  3. 字体颜色也应该是attributes字典的一部分。

例如:

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];

如果在您执行此操作后它仍未居中,您需要确认您正在渲染它的UIViewframe,并确保它位于您认为应该在的位置。您可以在调试器中运行应用程序,然后使用视图调试器 来确认视图所在的位置。

【讨论】:

    【解决方案2】:

    其他人都说你的代码基本正确,那为什么没有在你期望的地方显示文字呢?

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

    使用当前视图的边界而不是屏幕将使您的文本居中在该视图中,如果视图本身在屏幕上居中,这很好,但如果不是,也会显示偏离中心。 p>

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

    要在屏幕中心绘制文本,无论视图如何,您都可以像在 screen 坐标中一样计算原点,然后将其转换为视图的坐标并在那里绘制。例如。如果您的代码在视图的 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 ...];
    

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

    HTH

    【讨论】:

    • 在这个分屏多任务处理的时代,我认为你永远不想使用屏幕尺寸来确定任何东西的去向......
    • @Rob - 公平点,OP 最好使用情节提要的主视图。但无论哪种方式,看起来 OP 要么没有在他们认为的视图中绘制,要么在视图的错误坐标系中绘制 - 这就是他们的文本偏离中心的原因。仅仅使用视图边界不一定能解决这个问题(例如,如果视图没有居中)。
    • 我会在细节上狡辩(拆分视图控制器或其他视图控制器包含场景呢),但我绝对同意您的诊断,即这可能是坐标系转换错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2017-03-26
    • 2018-04-26
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2014-03-31
    相关资源
    最近更新 更多