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

来源:互联网 发布:飞跃车主采集软件 编辑:程序博客网 时间:2024/06/10 17:06

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

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

2、对齐类型:

typedef NS_ENUM(NSUInteger,textAlignType){  textAlignType_top = 10,   //顶部对齐  textAlignType_left,       //左边对齐  textAlignType_bottom,     //底部对齐  textAlignType_right,      //右边对齐  textAlignType_center      //水平/垂直对齐(默认中心对齐)};

3、创建一个ZWVerticalAlignLabel继承UILabel,在ZWVerticalAlignLabel中重写这两个方法,根据对齐类型分别进行设置。

主要代码:

ZWVerticalAlignLabel.m

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];    if (self.typeArray){        for (int i=0; i<self.typeArray.count; i++) {        textAlignType type = [self.typeArray[i] integerValue];            switch (type) {                case textAlignType_top:  //顶部对齐                    self.hasTop = YES;                    textRect.origin.y = bounds.origin.y;                    break;                case textAlignType_left: //左部对齐                    self.hasLeft = YES;                    textRect.origin.x = bounds.origin.x;                    break;                case textAlignType_bottom: //底部对齐                    self.hasBottom = YES;                    textRect.origin.y = bounds.size.height                     - textRect.size.height;                    break;                case textAlignType_right: //右部对齐                    self.hasRight = YES;                    textRect.origin.x = bounds.size.width                     - textRect.size.width;                    break;                case textAlignType_center:                    if (self.hasTop) {  //上中                        textRect.origin.x = (bounds.size.width                         - textRect.size.width)*0.5;                    }                    else if (self.hasLeft) { //左中                     textRect.origin.y = (bounds.size.height                      - textRect.size.height)*0.5;                    }                    else if (self.hasBottom) { //下中                        textRect.origin.x = (bounds.size.width                         - textRect.size.width)*0.5;                    }                    else if (self.hasRight) { //右中                        textRect.origin.y = (bounds.size.height                         - textRect.size.height)*0.5;                    }                    else{   //上下左右居中                       textRect.origin.x = (bounds.size.width                        - textRect.size.width)*0.5;                        textRect.origin.y = (bounds.size.height                          - textRect.size.height)*0.5;                    }                    break;                default:                    break;            }        }    }    return textRect;}-(void)drawTextInRect:(CGRect)requestedRect {    CGRect actualRect = requestedRect;    if (self.typeArray) {      actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];    }    [super drawTextInRect:actualRect];}

调用方法:

[label textAlign:^(ZWMaker *make) {                 make.addAlignType(textAlignType_bottom).addAlignType(textAlignType_right);   }];
  • make.addAlignType(textAlignType_bottom); //底部对齐
  • make.addAlignType(textAlignType_top); //顶部对齐
  • make.addAlignType(textAlignType_left); //左端对齐
  • make.addAlignType(textAlignType_right); //右端对齐
  • make.addAlignType(textAlignType_top).addAlignType(textAlignType_center); //顶部中间对齐
  • ….

效果图:
这里写图片描述

这里写图片描述

这里写图片描述

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

0 0
原创粉丝点击