2017082043372TypographyGraphic_2x.png 图片来自https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/

2017082080851cha_blog_2.png

实现底部对齐

如果在autolayout中实现label底部对齐,可以使用baselines来实现。 20170820338132.png 20170820850651.png

实现不同字体大小的label顶部对齐

重写label的alignmentRectInsets方法:

//objc
- (UIEdgeInsets)alignmentRectInsets{
    return UIEdgeInsetsMake(self.font.ascender - self.font.capHeight, 0, 0, 0);
}

//swift
override var alignmentRectInsets: UIEdgeInsets {
   return UIEdgeInsets(top: font.ascender - font.capHeight, left: 0, bottom: 0, right: 0)
}

效果如图:

201708205822822.png

参考: https://stackoverflow.com/questions/38024829/extend-uilabel-to-remove-ascender