UIFont属性探究

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中如何正确的实现行间距与行高

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容