zoukankan      html  css  js  c++  java
  • iOS 学习

    思路:计算文字的高度,存进数组

    加注:存在中文,需要加一行文字的高度,也就是 font

    主要代码

    #pragma mark -- UITableViewDelegate
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSNumber *height = self.heightArray[indexPath.row];
        //含有中文需要加一行字体的高度,也就是 font
        return height.floatValue;
    }
    //存储计算出来的高度
    - (NSMutableArray *)heightArray {
        if (!_heightArray) {        
            _heightArray = [NSMutableArray arrayWithCapacity:0];
            for (int i = 0; i < self.dataSource.count; i++) {
                NSString *str = [NSString stringWithFormat:@"%@",_dataSource[i]];
                CGFloat height = [HeightModel calculate:str];
                [_heightArray addObject:[NSNumber numberWithFloat:height]];
            }
        }
        return _heightArray;
    }
    //计算文字高度
    + (CGFloat)calculate:(NSString *)text {
    
        CGSize maxSize = CGSizeMake(KScreenWidth, MAXFLOAT);
        CGSize trueSize = [text boundingRectWithSize:maxSize
                                             options:
                           (NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin)
                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}
                                             context:nil].size;
        NSLog(@"%f",trueSize.height);
        return trueSize.height+15;
    }

     介绍一个类库:SDAutoLayout,比自己造的轮子好多了

  • 相关阅读:
    Unity做AR
    Linux怎么安装vim编译器
    Linux命令之tar
    Linux命令之ln
    Linux命令之grep
    Linux命令之less
    Linux命令之cd
    Linux命令之ll
    Linux命令之cp
    Linux命令之rm
  • 原文地址:https://www.cnblogs.com/asamu/p/5391430.html
Copyright © 2011-2022 走看看