您的位置:首页 > 产品设计 > UI/UE

UILable的对齐方式(上对齐,下对齐,默认只能中间对齐)

2016-08-25 16:20 531 查看
.h文件

#import <UIKit/UIKit.h>

typedef enum

{

    VerticalAlignmentTop,

    VertiaclAlignmentBottom,

    VerticalAlignmentMiddle

    

}VerticalAlignment;

@interface MyLable :
UILabel

@property(assign,nonatomic)
VerticalAlignment vertical;

@end

.m文件

#import "MyLable.h"

@implementation MyLable

-(void)setVertical:(VerticalAlignment)vertical

{

    _vertical = vertical;

    [self
setNeedsDisplay];

}

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

{

    CGRect rect = [super
textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];

    switch (_vertical) {

        case
VerticalAlignmentTop:

            rect.origin.y = bounds.origin.y;

            break;

        case
VertiaclAlignmentBottom:

            rect.origin.y = bounds.size.height - rect.size.height;

            break;

        default:

            rect.origin.y = (bounds.size.height - rect.size.height)/2;

            break;

    }

    return rect;

}

-(void)drawTextInRect:(CGRect)rect

{

    CGRect rectt = [self
textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];

    [super drawTextInRect:rectt];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: