根据NSString内容长度,计算Cell高度方法

2016-07-04  本文已影响103人  JasonEVA

在开发的过程中经常会遇到需要根据数据内容的长度来动态调整CELL或者View的高度,有以下几种方法:
方法一:
适用于普通string的显示,直接根据字大小及数量和最大宽度计算高度

#define W_MINHEIGHT 44   //最小高度
#define W_MAX   ([ [ UIScreen mainScreen ] bounds ].size.width - 60) // 文字最大宽度
#define OFFSET 8       // 偏移量

- (CGFloat)getRowHeightWithString:(NSString *)content
{
    // 得到输入文字内容长度
    NSString *text = (NSString *)content;
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@" "];
    UIFont *font = [UIFont systemFontOfSize:15];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    CGSize size = [text boundingRectWithSize:CGSizeMake(W_MAX, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    CGFloat height = 0;
    return height = MAX(size.height + OFFSET, W_MINHEIGHT);
}

方法二:
适用于需要设置行间距NSMutableAttributedString,使用systemLayoutSizeFittingSize方法根据cell的contentView的高度来设置tableview中cell的高度

#define W_MINHEIGHT 44   //最小高度

- (CGFloat)getRowHeightWithString:(NSMutableAttributedString *)content
{
    // 得到输入文字内容长度
    static MissionRemarkAndPatientTableViewCell *templateCell;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        templateCell = [self.tableView dequeueReusableCellWithIdentifier:[MissionRemarkAndPatientTableViewCell identifier]];
        if (!templateCell) {
            templateCell = [[MissionRemarkAndPatientTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[MissionRemarkAndPatientTableViewCell identifier]];
        }
    });
    templateCell.contentLb.attributedText = content;
    
    CGFloat height = [templateCell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize].height;
    return MAX(W_MINHEIGHT, height) > W_MINHEIGHT ? height + 9 : W_MINHEIGHT;
}

在对应的cell中需要设置label的属性

//
//  MissionRemarkAndPatientTableViewCell.m
//  HMDoctor
//
//  Created by jasonwang on 16/7/7.
//  Copyright © 2016年 yinquan. All rights reserved.
//

#import "MissionRemarkAndPatientTableViewCell.h"

#define W_MAX   ([ [ UIScreen mainScreen ] bounds ].size.width - 90)   // 文字最大宽度

@interface MissionRemarkAndPatientTableViewCell()

@end

@implementation MissionRemarkAndPatientTableViewCell
+ (NSString *)identifier { return NSStringFromClass([self class]);}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.titelLb];
        [self.contentView addSubview:self.contentLb];
    
        [self.titelLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(12);
            make.left.equalTo(self.contentView).offset(15);
            make.width.equalTo(@40);
        }];
        
        [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titelLb).priorityMedium();
            make.left.equalTo(self.titelLb.mas_right).offset(20);
            make.right.equalTo(self.contentView).offset(-15);
            make.bottom.equalTo(self.contentView).offset(-3).priorityLow();
        }];
    }
    return self;
}

- (UILabel *)titelLb {
    if (!_titelLb) {
        _titelLb = [UILabel new];
        [_titelLb setFont:[UIFont systemFontOfSize:15]];
        [_titelLb setTextColor:[UIColor commonDarkGrayColor_666666]];
    }
    return _titelLb;
}

- (UILabel *)contentLb {
    if (!_contentLb) {
        _contentLb = [UILabel new];
        [_contentLb setFont:[UIFont systemFontOfSize:15]];
        [_contentLb setTextColor:[UIColor commonBlackTextColor_333333]];
        [_contentLb setNumberOfLines:0];
        [_contentLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        [_contentLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        _contentLb.lineBreakMode = NSLineBreakByWordWrapping;
        _contentLb.preferredMaxLayoutWidth = W_MAX;
    }
    return _contentLb;
}
@end
上一篇 下一篇

猜你喜欢

热点阅读