Understanding UIFont(UIFont的各个属性解释)

Understanding UIFont

Have you ever really TRULY looked at the documentation of UIFont?

I had to, because I was looking for some metrics information that I could use to custom draw UILabels. And if you want to be independent of what font is set, then you have to get certain metrics, but the Apple SDK documentation of UIFont leaves a bit to desire.

When I asked which characters would be good representatives of a font, I was sent a link to this blog “celebrating the beauty of the ampersand”. Fine, an ampersand will be there as well…

Tethras App Localization - Start Today!

To visualize where each font metric belongs to I built assembled an app to show you what metric is which.

The font in the graphic is what you get by [UIFont boldSystemFontOfSize:56.0]. The Point Size is the only metric you have an influence over when you get a new font instance by means of any of the factory methods available. All the other values derive from this. I was most confused by the ascender and descender until I realized that ascender + 1 + descender is equal to the Point Size.

The baseline is the line on which all letters are standing. Leading is the measurement from one baseline to the next if you have text that is rendered on multiple lines. Finally you also get the height of captial letters and the height of the lowercase x. Why this is specifically singled out like this I cannot say, seems a bit unfair to have the small x represent all lowercase characters.

Helvetica is the font you generally get by the systemFont methods and rightfully so, Helvetica is a beautiful cut, so handsome in fact that it has it’s own movie. (Which is highly acclaimed BTW) It’s fine for use in most productivity apps and everything that wants to be compatible with the Apple look. But when you start using UILabels in games you will probably feel an urge to be somewhat more adventurous.

A more useful description

Being the seasoned Cocoa geek that we all are the first thing to try on any object is to see if it implements a useful description. Tuns out, UIFont does not. I tried to If you NSLog %@ the UIFont instance that is attached to a detailTextLabel of a subtitled tableViewCell. All you get is:

<UICFFont: 0x38898e0>

We can absolutely do better. Let’s add a category extension to UIFont.

UIFont+Description.h

#import 
 
@interface UIFont (Description)
 
@end

UIFont+Description.m

#import "UIFont+Description.h"
 
@implementation UIFont (Description)
 
- (NSString *)description
{
	return [NSString stringWithFormat:@"", self.fontName, self.pointSize];
}
 
@end

Note, that you don’t even need to add an import for this category to any class. Because EVERY descendant of NSObject has a description method this category extension actually overrules the existing one.

Ok, second try, again I want to know what font Apple is using for those subtitles:

cell.detailTextLabel.text = @"Some Text";
NSLog(@"%@", cell.detailTextLabel.font); // use our new description
}

We get a bit more info, but still there is a creepy feeling of Apple black magic, because for some strange reason the point size reported is 0, as if the font were undead, a floating spirit without a body.

<UICFFont: 'Helvetica-Bold', 0.00 point>

Clearly it displays, but how can this be without a size? Well, we instinctively tried theNSLog cellForRowAtIndexPath, but there is a better place to peak in the very last second before the tableview cell is displayed, in willDisplayCell. There we finally get the result we expect:

<UICFFont: 'Helvetica', 14.00 point>

That’s more like it.

All Fonts available on iPhone

The iPhone (currently) does not have any mechanisms (yet) to add custom fonts. This might or might not change (hint, hint) with the new SDK version that also supports the iPad. But for the time being and if you want to support all existing iDevices out there you have to be content with what font come preinstalled.

UIFonts are categorized in Font Family Names which group together different font cuts like oblique or bold. I made a tableview with all possible combinations, sorted by family name and rendered it into this bitmap for reference.

from: http://www.cocoanetics.com/2010/02/understanding-uifont/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值