首页 > 代码库 > lable自适应宽度和高度
lable自适应宽度和高度
NSString *nameString = self.dataArray[indexPath.row][@"user"];
NSDictionary *nameLabelAttr=@{NSFontAttributeName:LLShowMessageNameFont};
//// 自适应宽度
float rightNameWidth=[nameString sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(1000, 20)].width;
//// 自适应高度
CGSize nameLabelMaxSize=CGSizeMake(rightNameWidth, MAXFLOAT);
CGSize nameLabelSize=[nameString boundingRectWithSize:nameLabelMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nameLabelAttr context:nil].size;
CGFloat nameLabelX = CGRectGetMaxX(cell.iconView.frame) + LLShowMessageCellBorder*kWidthScale;
CGFloat nameLabelY = cell.iconView.origin.y;
cell.nameLabel.frame = (CGRect){{nameLabelX, nameLabelY}, nameLabelSize};
cell.nameLabel.text = nameString;
////如果出现了文字不能全部显示,需要加上一下代码,即可
[cell.nameLabel sizeToFit];
lable自适应宽度和高度