zoukankan      html  css  js  c++  java
  • UILabel顶端对齐

    比较一劳永逸的写法是对label添加一个分类

    @interface UILabel (VerticalAlign)
    /** 顶端对齐 */
    -(void)alignTop;
    /** 底部对齐 */
    -(void)alignBottom;
    @end
    -(void)alignTop{
        CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
        double finalHeight = fontSize.height * self.numberOfLines;
        double finalWidth = self.frame.size.width;
        CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight)
                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                    attributes:@{NSFontAttributeName:self.font}
                                                       context:nil].size;
        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 sizeWithAttributes:@{NSFontAttributeName:self.font}];
        double finalHeight = fontSize.height * self.numberOfLines;
        double finalWidth = self.frame.size.width;
        CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight)
                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                    attributes:@{NSFontAttributeName:self.font}
                                                       context:nil].size;
        int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
        for(int i=0; i<newLinesToPad; i++){
            self.text = [NSString stringWithFormat:@" 
    %@",self.text];
        }
    }

    然后在使用时添加[myLabel alignTop]即可。
    添加前

    使用后

  • 相关阅读:
    糗事百科图片爬取
    Linux文件和目录常用命令
    复习
    Win7的快捷键
    开始运行中的快捷键
    TextBox客户端JS赋值 后台获取(转载)
    window.returnValue的用法
    input的readonly属性与TextBox的ReadOnly和Enabled属性区别
    剖析 ADO.NET 批处理更新
    关于C#多线程的WaitHandle
  • 原文地址:https://www.cnblogs.com/Apologize/p/5908537.html
Copyright © 2011-2022 走看看