UIFont属性探究

2018-10-27  本文已影响17人  c048e8b8e3d7

UIFont最常用的属性就是fontNamefontSize
下面的几个属性不常用但是跟文字的绘制有关

// Font attributes
@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;//小写x的高度
@property(nonatomic,readonly)        CGFloat   lineHeight NS_AVAILABLE_IOS(4_0);//当前字体下的行高
@property(nonatomic,readonly)        CGFloat   leading;行间距(一般为0)
结合图片来看,属性的作用会更加容易理解

所以得以得到以下结论

  1. pointSize = fontSize 即字体大小
  2. lineHeight = ascender + descender
  3. 设置富文本行高为0的时候,依然可以看到行与行之间存在间隙,间隙的高度为lineHeight - pointSize,
  4. 设置富文本的时候,如果文本只有一段,那么计算出来的文本所需高度是lineHeight + lineSpacing;如果有N(N>1)行,那么计算出来的文本所需高度为 lineHeight * N + lineSpacing * (N - 1)

参考

Understanding fonts and UIFont
在iOS中如何正确的实现行间距与行高

上一篇 下一篇

猜你喜欢

热点阅读