iOS 对UIFont的新的

作者: 雪中夜归人 | 来源:发表于2017-05-09 16:43 被阅读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;
  • familyName 字体家族的名字
  • fontName 字体的名字
  • pointSize 字体大小
  • ascender 基准线以上的高度
  • descender 基准线以下的高度
  • capHeight 大小的高度
  • xHeight 小写x的高度
  • lineHeight 当前字体下的行高
  • leading 行间距(一般为0)

具体代码运行结果可以更加清楚的说明各个属性的值,代码以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,在富文本中设置行高的时候,其实际文字间的距离就是加上这个距离的。(原来一直错误的理解文字间的距离就是行间距)

相关文章

网友评论

    本文标题: iOS 对UIFont的新的

    本文链接:https://www.haomeiwen.com/subject/yhrstxtx.html