【发布时间】: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