iOS UILabel垂直对齐(上居中)

5,683 阅读1分钟

项目中显示文本信息的时候,大多使用Label。但是当文字量比较小的时候,我们定义label.frame较大,这时候文字会自动上下居中显示,不符合我们的要求,并且UIlabel也没有提供相应的属性或者方法来满足我们的要求。为了满足需求,我之前的方法都是计算文字的长度,然后再设置label的frame。后来看到了一篇文章,实现了label的上居中。自己实现了一下,基本满足的需求,下面就是实现一个简单的垂直居中的label。

YXLabel.h

#import <UIKit/UIKit.h>

typedef NS_ENUM (NSInteger ,VerticalAlignment){
    
    VerticalAlignmentTop = 0,  //上居中
    
    VerticalAlignmentMiddle, //中居中
    
    VerticalAlignmentBottom //低居中
    
};
//继承UILabel
@interface YXLabel : UILabel
//垂直居中属性
@property (nonatomic,assign)VerticalAlignment verticalAlignment;

@end

YXLabel.m

#import "YXLabel.h"

@implementation YXLabel

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.verticalAlignment = VerticalAlignmentMiddle;
        
    }
    
    return self;
    
}
/**
 *  设置属性方法
 *
 *  @param verticalAlignment 垂直调整位置
 */
-(void)setVerticalAlignment:(VerticalAlignment)verticalAlignment{
    
    _verticalAlignment = verticalAlignment;
    
    [self setNeedsDisplay];
    
}
/**
 *  计算文字的矩形区域
 *
 *  @param bounds        label矩形区域
 *  @param numberOfLines 行数
 *
 *  @return 返回文字所占的矩形区域
 */
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines{
    
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];

    //通过设定字体区域的y值来调整垂直位置
    switch (self.verticalAlignment) {
            
        case VerticalAlignmentTop:
            
            textRect.origin.y = self.bounds.origin.y;
            
            break;
            
        case VerticalAlignmentMiddle:
            
            break;
            
        case VerticalAlignmentBottom:
            
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            
            break;
            
        default:
            
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
            
            break;
            
    }
    
    return textRect;
    
}
//重写父类方法
-(void)drawTextInRect:(CGRect)rect{
    
    CGRect actualRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
    
    [super drawTextInRect:actualRect];
    
}