UILabel 根据文本内容设置frame
Posted frounk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UILabel 根据文本内容设置frame相关的知识,希望对你有一定的参考价值。
CGRect senderFrame = cell.senderLabel.frame;
CGRect creatAtFrame = cell.creatAtLabel.frame;
CGSize senderSize = [cell.senderLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:15.0]}];
senderFrame.size.width = ceil(senderSize.width);
if (senderFrame.size.width>0) {
senderFrame.size.width=ceil(senderSize.width)+10;
}
if (senderFrame.size.width>200) {
senderFrame.size.width = 200;
}
cell.senderLabel.frame = senderFrame;
creatAtFrame.origin.x = CGRectGetMaxX(cell.senderLabel.frame)+10;
cell.creatAtLabel.frame = creatAtFrame;
NSDictionary *info = messageModel.info;
// 设置属性来设置行高
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 3;
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.0],NSParagraphStyleAttributeName:paragraphStyle};
CGFloat width = CGRectGetWidth(self.view.frame)-CGRectGetMinX(cell.senderLabel.frame) - 16;
CGRect frame = cell.descLabel.frame;
frame.size.width = width;
cell.descLabel.emojiDelegate = self;
[cell.descLabel sizeToFit];
// cell.descLabel.text = info[@"message"];
[cell.descLabel setEmojiText:messageModel.timeViewStr];
CGRect bodyFrame = CGRectMake(0, 0, 0, 0);
// if (cell.descLabel.text!=NULL) {
bodyFrame = [cell.descLabel.text boundingRectWithSize:CGSizeMake(width, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
frame.size.height = ceil(bodyFrame.size.height);
cell.descLabel.frame = frame;
以上是关于UILabel 根据文本内容设置frame的主要内容,如果未能解决你的问题,请参考以下文章
iOS UILabel讲解以及根据字符串长度自动适应宽度和高度