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
  • 相关阅读:
    Redis 简介
    图片懒加载、selenium和PhantomJS
    Python网络爬虫之三种数据解析方式
    Scrapy框架之CrawlSpider
    Scrapy 框架
    python 网络爬虫概念与HTTP(s)协议
    Mysql概念及基本操作
    Python re 模块
    线程与进程应用场景
    全局解释器锁 GIL
  • 原文地址:https://www.cnblogs.com/hushuai-ios/p/3679783.html
Copyright © 2011-2022 走看看