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,比自己造的轮子好多了

  • 相关阅读:
    pyhanlp 实体命名识别
    NABCD需求分析
    源代码
    遇到的问题和解决方法
    运行及总结
    测试与调试
    读《一个程序猿的生命周期》和《人,绩效和职业道德》有感
    面向对象程序设计
    设计类图
    SRS文档
  • 原文地址:https://www.cnblogs.com/asamu/p/5391430.html
Copyright © 2011-2022 走看看