zoukankan      html  css  js  c++  java
  • 设置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];
  • 相关阅读:
    Alook搭配JS脚本完美食用
    分享小米刷机教程/线刷(工具支持小米华为一加)
    iPhone 无需越狱修改wx+zfb+qq步数
    如果SELECT语句中没有结果,则使用CASE返回字符串
    MSSQLServer 正在显示"正在还原...."
    C# 小技巧
    C#使用Select方法快速获取List集合集合中某个属性的所有值集合
    C#中使用Sum方法对List集合进行求和操作
    sql日期函数
    sql只根据某一字段去重,并保留其他字段
  • 原文地址:https://www.cnblogs.com/CodingMann/p/5120296.html
Copyright © 2011-2022 走看看