zoukankan      html  css  js  c++  java
  • 控制UIlabel 垂直方向对齐方式的 方法

    最正统的方法,利用objective-c的category特性,修改UILabel的绘制代码。示例代码如下:

    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
    
    // -- file: UILabel+VerticalAlign.h
    #pragma mark VerticalAlign
    @interface UILabel (VerticalAlign)
    - (void)alignTop;
    - (void)alignBottom;
    @end
    // -- file: UILabel+VerticalAlign.m
    @implementation UILabel (VerticalAlign)
    - (void)alignTop {
        CGSize fontSize = [self.text sizeWithFont:self.font];
        double finalHeight = fontSize.height * self.numberOfLines;
        double finalWidth = self.frame.size.width;    //expected width of label
        CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
        int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
        for(int i=0; i<newLinesToPad; i++)
            self.text = [self.text stringByAppendingString:@"
     "];
    }
    - (void)alignBottom {
        CGSize fontSize = [self.text sizeWithFont:self.font];
        double finalHeight = fontSize.height * self.numberOfLines;
        double finalWidth = self.frame.size.width;    //expected width of label
        CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
        int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
        for(int i=0; i<newLinesToPad; i++)
            self.text = [NSString stringWithFormat:@" 
    %@",self.text];
    }
    @end
    

     

  • 相关阅读:
    docker stats
    Appium 环境搭建
    docker 进入容器
    Mac下Mysql启动异常["ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"]
    python 虚拟环境--virtualenv
    visjs 绘图 图标 动态添加数据
    js 滑块登录验证
    js iframe 最顶层显示
    转化为数组
    videojs双击全屏幕观看,videojs动态加载视频
  • 原文地址:https://www.cnblogs.com/liyufeng2013/p/UILabel.html
Copyright © 2011-2022 走看看