zoukankan      html  css  js  c++  java
  • UILabel居上对齐居下对齐类别扩展

    实现原理:当该label不足以达到要求的高度时,居上对齐时在页末补“ 空格”,居下对齐时在页首补“空格 ”

    UILabel+VerticalAlign.h

    @interface UILabel(VerticalAlign)
    -(void)alignTop;
    -(void)alignBottom;
    @end

    UILabel+VerticalAlign.m

    @implementation UILabel(VerticalAlign)
    -(void)alignTop {
        CGSize fontSize =[self.text sizeWithFont:self.font];
        double finalHeight = self.frame.size.height;//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
  • 相关阅读:
    rem适配布局---5. 方案1:苏宁首页制作1
    rem适配布局---4. rem适配方案
    rem适配布局---3. less
    rem适配布局---2. 媒体查询
    rem适配布局---1. 基础
    flex布局---9.携程网案例
    java基础---3. 数据类型转换、运算符
    flex布局---8.flex布局原理
    java基础---2. 常量&变量
    工会项目结题,游泳锻炼
  • 原文地址:https://www.cnblogs.com/hushuai-ios/p/3675042.html
Copyright © 2011-2022 走看看