iOS UILabel垂直居中

平时开发的时候可能会遇到这种问题:当一个UILabel的frame的高度设置的过大时,发现UILabel是垂直居中的,有的需求是需要将这个Label垂直向上显示,之前的办法是计算出label.text的字体所占用的frame大小,根据这个大小再重新设置label的frame值,未免有些麻烦,前阵子封装了个自定义label实现的垂直居中的设置。废话不多说,上代码。

//
//  JFLabel.h
//  BobcareDoctorApp
//
//  Created by Japho on 16/2/25.
//  Copyright © 2016年 com.01wisdom. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum
{
    VerticalAlignmentTop = 0, // default
    VerticalAlignmentMiddle,
    VerticalAlignmentBottom,
} VerticalAlignment;

@interface JFLabel : UILabel
{
    
@private
    
    VerticalAlignment _verticalAlignment;
}

@property (nonatomic) VerticalAlignment verticalAlignment;

@end

//
//  JFLabel.m
//  BobcareDoctorApp
//
//  Created by Japho on 16/2/25.
//  Copyright © 2016年 com.01wisdom. All rights reserved.
//

#import "JFLabel.h"

@implementation JFLabel

@synthesize verticalAlignment = verticalAlignment_; 

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.verticalAlignment = VerticalAlignmentMiddle;
    }
    return self;
}

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
    verticalAlignment_ = verticalAlignment;
    [self setNeedsDisplay];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    switch (self.verticalAlignment) {
        case VerticalAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case VerticalAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case VerticalAlignmentMiddle:
            // Fall through.
        default:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
    }
    return textRect;
}

-(void)drawTextInRect:(CGRect)requestedRect {
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    [super drawTextInRect:actualRect];
}

@end

封装的类继承自UILabel,需要设置垂直居中时直接设置属性就可以了。

调用示例代码:

- (JFLabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[JFLabel alloc] initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH - CASE_IMAGE_VIEW_WIDTH - 15 - 20 - 5, 40)];
        _titleLabel.text = @"测试垂直居中文字";
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.numberOfLines = 0;
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.verticalAlignment = VerticalAlignmentTop;//垂直居中
    }
    
    return _titleLabel;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值