-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUIVerticalAlignmentLabel.m
40 lines (34 loc) · 1.23 KB
/
UIVerticalAlignmentLabel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// UIVerticalAlignmentLabel.m
// Coding_iOS
//
// Created by Ease on 15/1/7.
// Copyright (c) 2015年 Coding. All rights reserved.
//
#import "UIVerticalAlignmentLabel.h"
@implementation UIVerticalAlignmentLabel
- (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