开发者

Calculating UIFont size based on pre calculated ascender

I am using a custom font for my iOS application. My view needs to choose a different point size for the font depending on the size of the view. If I have a predetermined

CGFloat ascender;

and I essentially want to determine this

CGFloat calculatedFontSize = [self fontSizeForAscender:ascender];
[UIFont fontWithName:@"DINEngschriftStd" size:calculatedFontSize];

what would be the best way to accom开发者_Go百科plish this?


try this. [aString sizeWithFont:font minFontSize:minSize actualFontSize:&theSizeToUse forWidth:ViewWidth lineBreakMode:UILineBreakModeWordWrap];

theSizeToUse is just an uninitialized CGFloat (ie. CGFloat theSizeToUse;). You should pass it's address to the method, and on return it will contain the font size you should use.


There isn't a particularly obvious way to do so. You can iteratively change the font size until you find one with the correct ascender, but it's a bit messy (fortunately, there aren't that many floats so you can do it reasonably accurately in 31 trials with interval bisection).

On the other hand, if you assume that the ascender scales linearly with the font size, you can just do this:

UIFont * f = [UIFont fontWithName:... size:1];
f = [f fontWithSize:ascender/f.ascender];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜