图片来自https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/
实现底部对齐
如果在autolayout中实现label底部对齐,可以使用baselines来实现。
实现不同字体大小的label顶部对齐
重写label的alignmentRectInsets
方法:
- (UIEdgeInsets)alignmentRectInsets{
return UIEdgeInsetsMake(self.font.ascender - self.font.capHeight, 0, 0, 0);
}
override var alignmentRectInsets: UIEdgeInsets {
return UIEdgeInsets(top: font.ascender - font.capHeight, left: 0, bottom: 0, right: 0)
}
效果如图:
参考: https://stackoverflow.com/questions/38024829/extend-uilabel-to-remove-ascender