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];
  • 相关阅读:
    传输速率转换
    CentOS7 安装 oracleXE(快捷版)教程
    QEMU/KVM虚拟机安装配置
    Nginx 1.10.1 编译、配置文档(支持http_v2,TLSv1.2,openssl v1.0.2)
    IDC机房线路质量测试方案
    Iptables防火墙NAT地址转换与端口转发
    Zabbix使用点滴
    MySQL基本命令
    防DDOS攻击SHELL脚本
    12月份的英文简写
  • 原文地址:https://www.cnblogs.com/CodingMann/p/5120296.html
Copyright © 2011-2022 走看看