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
  • 相关阅读:
    【计算机网络】宽带、基带传输
    【操作系统】多道程序的理解
    【操作系统】操作系统的理解
    NLP学习常用的网页链接
    linux下常用FTP命令 1. 连接ftp服务器[转]
    shell运行java/Jar 脚本
    jsp验证码
    用javascript实现的验证码
    eclipse设置高亮显示的颜色
    oracle 导出
  • 原文地址:https://www.cnblogs.com/hushuai-ios/p/3679783.html
Copyright © 2011-2022 走看看