zoukankan      html  css  js  c++  java
  • 分享一个可垂直顶端对齐的UILabel

    1.头文件

    #import <Foundation/Foundation.h>
    typedef enum VerticalAlignment {
        VerticalAlignmentTop,
        VerticalAlignmentMiddle,
        VerticalAlignmentBottom,
    } VerticalAlignment;

    @interface VerticallyAlignedLabel : UILabel 
    {
    @private
        VerticalAlignment verticalAlignment_;
    }

    @property (nonatomic, assign) VerticalAlignment verticalAlignment;

    @end 


    .M文件

    #import "VerticallyAlignedLabel.h"


    @implementation VerticallyAlignedLabel

    @synthesize verticalAlignment = verticalAlignment_;

    - (id)initWithFrame:(CGRect)frame 
    {
        self = [super initWithFrame:frame];
        if (self) 
        {
            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 

  • 相关阅读:
    从PowerDesigner概念设计模型(CDM)中的3种实体关系说起
    详细整数分区的问题解释
    JavaWeb显示器
    mysql中国的内容php网页乱码问题
    Linux开发环境的搭建和使用——Linux本必备软件SSH
    【Android UI设计和开发】动画(Animation)详细说明(一)
    应该是什么在预新手发“外链”(4)最终的外链的方法
    选择29部分有用jQuery应用程序插件(免费点数下载)
    设计模式--装饰图案
    Webserver管理系列:3、Windows Update
  • 原文地址:https://www.cnblogs.com/likwo/p/2446278.html
Copyright © 2011-2022 走看看