zoukankan      html  css  js  c++  java
  • 自适应label的高度

    iOS7以下的系统可使用方法

    //获得当前cell高度

        CGRect frame = [self frame];

        //文本赋值

        self.introduction.text = text;

        //设置label的最大行数

        self.introduction.numberOfLines = 0;

        CGSize size = CGSizeMake(300, 1000);

        CGSize labelSize = [self.introduction.text sizeWithFont:self.introduction.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];

        NSLog(@"w = %f", labelSize.width);

        NSLog(@"h = %f", labelSize.height);

       

    iOS7以上才支持的方法

    //    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:self.introduction.text];

    ////    self.introduction.attributedText = attrStr;

    //    NSRange range = NSMakeRange(0, attrStr.length);

    //    NSDictionary *dic = [attrStr attributesAtIndex:0 effectiveRange:&range];

        NSDictionary *dic = @{NSFontAttributeName: self.introduction.font};

        CGSize labelSize1 = [self.introduction.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;

        NSLog(@"w = %f", labelSize1.width);

        NSLog(@"h = %f", labelSize1.height);

        

        self.introduction.frame = CGRectMake(self.introduction.frame.origin.x, self.introduction.frame.origin.y, labelSize1.width, labelSize1.height);

        

        //计算出自适应的高度

        frame.size.height = labelSize1.height+100;

        

        self.frame = frame;

        

    iOS6以上才支持的方法

        NSAttributedString *str = [[NSAttributedString alloc] initWithString:self.introduction.text];

        NSAttributedString *str1 = [[NSAttributedString alloc] initWithString:self.introduction.text attributes:@{NSFontAttributeName : self.introduction.font}];

        

        CGSize size1 = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;

        CGSize size2 = [str1 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;

  • 相关阅读:
    跨域解决方法
    css之line-height
    untiy项目中使用MD5加密
    unity给子物体添加Shader
    unity中UI坐标转3d世界坐标
    unity项目字符串转为Vector3和Quaternion
    unity中使用Highlighting System v4.0插件给物体添加高亮
    加载AssetBundle方法
    Lua面向对象----类、继承、多继承、单例的实现
    Lua学习笔记(一)-----C#和lua的交互
  • 原文地址:https://www.cnblogs.com/wskgjmhh/p/4567789.html
Copyright © 2011-2022 走看看