自动适配文字高度
//UILabel文字自适应 - (UILabel *)titleL{ if(_titleL ==nil){ _titleL = [[UILabel alloc] init]; /**设置文字*/ [_titleL setText:self.myMessModel.content]; [_titleL setTextAlignment:NSTextAlignmentLeft]; //文字颜色 [_titleL setTextColor:[self.Skin getCellTitleColor]]; _titleL.numberOfLines = 0; //多行 //设置格式 NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading; [_titleL setFont:[self.Skin getDefaultTextFont]]; //SCREEN_SIZE.width 屏幕的宽度 WJ_HOME_LEFT_RIGHT_GAP间隙 MAXFLOAT 最大高度 CGRect contentRect = [_titleL.text boundingRectWithSize:CGSizeMake(SCREEN_SIZE.width - 2*WJ_HOME_LEFT_RIGHT_GAP, MAXFLOAT) options:options attributes:@{NSFontAttributeName: [self.Skin getDefaultTextFont]} context:nil]; [_titleL setFrame:CGRectMake(WJ_HOME_LEFT_RIGHT_GAP, CGRectGetMaxY(self.timeL.frame), contentRect.size.width, contentRect.size.height)]; } return _titleL; } //获取字符串的宽度 -(float) widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height { //此处的换行类型(lineBreakMode)可根据自己的实际情况进行设置 CGRect sizeToFit = [value boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil]; return sizeToFit.size.width; } //获得字符串的高度 -(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width { CGRect sizeToFit = [value boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, width) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil]; return sizeToFit.size.height; }
将来的自己,会感谢现在不放弃的自己!