zoukankan      html  css  js  c++  java
  • UILABEL 增加居上对齐 居下对齐方法

    .h文件

    #import <UIKit/UIKit.h>
    
    @interface UILabel(VerticalAlign)
    -(void)alignTop;
    -(void)alignBottom;
    @end

    .m文件

    #import "UILabel+VerticalAlign.h"
    
    @implementation UILabel(VerticalAlign)
    -(void)alignTop {
        CGSize fontSize = [@"一行" sizeWithFont:self.font constrainedToSize:CGSizeMake(self.frame.size.width, INT64_MAX) lineBreakMode:NSLineBreakByWordWrapping];//单行高度[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, 2000) 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
  • 相关阅读:
    Interesting Finds: 2008.03.19
    Interesting Finds: 2008.03.11
    Interesting Finds: 2008.03.27
    Interesting Finds: 2008.03.21
    每日日报
    每日日报
    每日日报
    每日日报
    idea怎么创建properties文件
    移动端rem.js
  • 原文地址:https://www.cnblogs.com/hushuai-ios/p/3679783.html
Copyright © 2011-2022 走看看