自定义UILabel,添加verticalAlignment属性

来源:互联网 发布:linux 执行二进制文件 编辑:程序博客网 时间:2024/05/30 05:42

效果

  • —–bottom屏幕快照 2017-12-13 下午1.58.47.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];// 获取文字的rect    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;}
原创粉丝点击