iOS UILabel垂直对齐(上居中)

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

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.mui

#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];
    
}

复制代码
相关文章
相关标签/搜索