美文网首页
UILabel 文字 置顶、居中、置底 2015-08-25

UILabel 文字 置顶、居中、置底 2015-08-25

作者: blaze冰叔 | 来源:发表于2019-07-19 14:54 被阅读0次
    //
    //  myUILabel.h
    //  IM_iphone
    //
    //  Created by blazce on 15/8/25.
    //
    //
    #import <UIKit/UIKit.h>
    #import <UIKit/UIKit.h>
    typedef enum
    {
        VerticalAlignmentTop = 0, // default
        VerticalAlignmentMiddle,
        VerticalAlignmentBottom,
    } VerticalAlignment;
    @interface myUILabel : UILabel
    {
    @private
        VerticalAlignment _verticalAlignment;
    }
    @property (nonatomic) VerticalAlignment verticalAlignment;
    @end
    
    //
    //  myUILabel.m
    //  IM_iphone
    //
    //  Created by blazce on 15/8/25.
    //
    //
    #import "myUILabel.h"
    @implementation myUILabel
    @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 文字 置顶、居中、置底 2015-08-25

          本文链接:https://www.haomeiwen.com/subject/eixslctx.html