IOS | MACiOS 你不知道的还很多iOS开发-属性含义与用法

iOS 对UIFont的新的

2017-05-09  本文已影响1084人  雪中夜归人

几乎是在入行的时候就已经开始接触UIFont这个类,但是从来没有细细去研究过它里边到底都有什么,最近工作中遇到了相关问题就顺道细细研究了一下。

单纯的使用这个类很简单,我主要说的是其中的属性。

@property(nonatomic,readonly,strong) NSString *familyName;
@property(nonatomic,readonly,strong) NSString *fontName;
@property(nonatomic,readonly)        CGFloat   pointSize;
@property(nonatomic,readonly)        CGFloat   ascender;
@property(nonatomic,readonly)        CGFloat   descender;
@property(nonatomic,readonly)        CGFloat   capHeight;
@property(nonatomic,readonly)        CGFloat   xHeight;
@property(nonatomic,readonly)        CGFloat   lineHeight ;
@property(nonatomic,readonly)        CGFloat   leading;

具体代码运行结果可以更加清楚的说明各个属性的值,代码以14号字体为例

UIFont *font = [UIFont systemFontOfSize:14];
NSLog(@"font.pointSize = %f,font.ascender = %f,font.descender = %f,font.capHeight = %f,font.xHeight = %f,font.lineHeight = %f,font.leading = %f",font.pointSize,font.ascender,font.descender,font.capHeight,font.xHeight,font.lineHeight,font.leading);

运行结果如下

font.pointSize = 14.000000,font.ascender = 13.330078,font.descender = -3.376953,font.capHeight = 9.864258,font.xHeight = 7.369141,font.lineHeight = 16.707031,font.leading = 0.000000

其中可以很明显的看到:

  1. 设置的字体大小就是 pointSize
  2. ascender + descender = lineHeight
    3.实际行与行之间就是存在间隙的,间隙大小即为 lineHeight - pointSize,在富文本中设置行高的时候,其实际文字间的距离就是加上这个距离的。(原来一直错误的理解文字间的距离就是行间距)
上一篇 下一篇

猜你喜欢

热点阅读