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

  • 相关阅读:
    otto-group-product-classification-challenge(Pytorch处理多分类问题)
    F1值
    win10 安装torch
    win10 安装 scrapy
    头条 街拍
    1029 Median
    欧拉回路
    Pre-Post
    Django 安装for Python3.4
    Python 安装for Windows
  • 原文地址:https://www.cnblogs.com/asamu/p/5391430.html
Copyright © 2011-2022 走看看