简体   繁体   中英

iphone - calculating the font size

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

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?

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.

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.

Any suggestions?

thanks for any help.

Simply setting uiLabel.adjustsFontSizeToFitWidth = YES will cause your text to scale down to fit the text field. Just make sure your font size is large enough for the iPad to begin with, because the text will never be scaled up .

Of course, you also need to make sure the UILabel itself is resized along with its superview, or neither the UILabel nor its contents will be scaled. You can do that using the Size tab in Interface Builder, or programatically using the UILabel's autoresizingMask property.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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