• 设置lable内容不上下居中


    转载自:http://dong-zsh.github.io/2015/10/14/%E8%AE%BE%E7%BD%AElable%E5%86%85%E5%AE%B9%E4%B8%8D%E4%B8%8A%E4%B8%8B%E5%B1%85%E4%B8%AD/

    #import <UIKit/UIKit.h>
    typedef enum
    {
    VerticalAlignmentTop = 0, // default文字居上
    VerticalAlignmentMiddle,
    VerticalAlignmentBottom,
    } VerticalAlignment;

    @interface mylable : UILabel

    {
    @private
    VerticalAlignment _verticalAlignment;
    }

    @property (nonatomic) VerticalAlignment verticalAlignment;

    @end
    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
    #import "mylable.h"

    @implementation mylable

    @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];
    }

    ###用法

    1
    2
    3
    4
    5
    6
    mylable *lable = [[mylable alloc] initWithFrame:CGRectMake(10, 400, 100, 200)];
    lable.text = @"dajdiojajdajdjajdadakdajdiaodaiojdioajdioajidjaijdajdadadna";
    lable.backgroundColor = [UIColor grayColor];
    lable.numberOfLines = 0;
    [lable setVerticalAlignment:VerticalAlignmentTop];
    [self.view addSubview:lable];
  • 相关阅读:
    Dapper的基本使用
    Dapper
    Dapper(一) 简介和性能
    Dapper入门使用,代替你的DbSQLhelper
    Dapper-小型ORM之王(C#.NET)
    Dos.Common
    Dos.ORM(原Hxj.Data)- 目录、介绍
    读写分离
    什么是长连接,什么是短连接?长连接和短连接的区别是什么?
    HTTP的长连接和短连接
  • 原文地址:https://www.cnblogs.com/CodingMann/p/5120296.html
Copyright © 2020-2023  润新知