效果
- —–bottom
![屏幕快照 2017-12-13 下午1.58.47.png](https://i-blog.csdnimg.cn/blog_migrate/979b800a153b4f8748e3fccd2d73e0b5.webp?x-image-process=image/format,png)
代码:
@synthesize verticalAlignment = _verticalAlignment
- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment
{
_verticalAlignment = verticalAlignment
[self setNeedsDisplay]
}
- (void)drawTextInRect:(CGRect)rect
{
CGRect actualRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines]
[super drawTextInRect:actualRect]
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]
textRect = CGRectMake(bounds.origin.x, textRect.origin.y, bounds.size.width, textRect.size.height)
switch (self.verticalAlignment) {
case KLTextVerticalAlignmentTop:
textRect.origin.y = bounds.origin.y
break
case KLTextVerticalAlignmentBottom:
textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height
break
case KLTextVerticalAlignmentMiddle:
// Fall through.
default:
textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0
break
}
return textRect
}