天天看点

UILabel设置顶、左、右、底部对齐

效果图:

UILabel设置顶、左、右、底部对齐

1、主要用到了两个方法和一个工具类ZWMaker

  • -(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

  • -(void)drawTextInRect:(CGRect)requestedRect

2、对齐类型

typedef NS_ENUM(NSUInteger, TextAlignType) {
  TextAlignType_top = 1,   // 顶部对齐
  TextAlignType_left,       // 左边对齐
  TextAlignType_bottom,     // 底部对齐
  TextAlignType_right,      // 右边对齐
  TextAlignType_center      // 水平/垂直对齐(默认中心对齐)
};
           

3、调用方式

[label textAlign:^(ZWMaker *maker) {
    make.addAlignType(TextAlignType_bottom).addAlignType(TextAlignType_center);
}];
 
[label textAlign:^(ZWMaker *maker) {
     maker.bottom().center();
 }];
           

项目地址:https://github.com/MisterZhouZhou/UILabelTextAlign

继续阅读