iPhone-计算字体大小 [英] iphone - calculating the font size

查看:101
本文介绍了iPhone-计算字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当视图为480x320时,我必须显示一个字体大小为14
的标签。

I have to show a label with font size = 14 when the view is 480x320.

假设我要这样做我的代码适用于所有未来设备的屏幕尺寸,包括iPad和以下的其他设备(我相信还会有更多),这样做的最佳方法是什么?

Supposing I would like to do this in a way that my code will be working well for all future device's screen sizes, including iPad and others following (I am sure more will come), what is the best way to do that?

我可以按比例进行此操作,我的意思是,如果缩放比例增加X,则字体X也增加,但是我担心的是设备的纵横比是否不同。

I could do this proportionally, I mean, if the scale increased X, increase the fonts X, but my concern is the different aspect ratios of the devices.

iPhone宽高比为1.5,但iPad的宽高比为1.33,其他宽高比也会出现...我不确定这种简单的缩放方法是否足以在所有设备上产生字体一致性。

The iPhone aspect ratio is 1.5, but iPad's is 1.33, and other aspect ratios can come... I am not sure if this simple scale method will be enough to produce font consistency across all devices.

有什么建议吗?

感谢您的帮助。

推荐答案

您可以尝试使用此代码。它将自动将文本缩小以适合特定宽度(此处为contentRect)。只需指定要使用的最小和最大可接受字体大小即可:

You could try to use this code. It will automatically scale your text down to fit within a certain width (here the contentRect). Just specify the minimum and maximum acceptable font size to use:

#define MAX_FONT_SIZE
#define MIN_FONT_SIZE
#define LEFT_TEXT_OFFSET
#define TEXT_TOP

UIFont *theFont = [UIFont systemFontOfSize: MAX_FONT_SIZE];
CGRect contentRect = self.bounds;
CGFloat boundsX = contentRect.origin.x;
CGPoint point;
CGSize width = contentRect.size.width;

point = CGPointMake(boundsX + LEFT_TEXT_OFFSET, TEXT_TOP);
[theText drawAtPoint:point forWidth:width withFont:theFont 
         minFontSize:MIN_FONT_SIZE actualFontSize:NULL
         lineBreakMode:UILineBreakModeTailTruncate
         baselineAdjustment:UIBaselineAdjustmentAlignBaselines];

Claus

这篇关于iPhone-计算字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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