Objective c sizeWithFont:和sizeWithAttributes:don';我一条线也干不了
我正在尝试为表格中的单元格设置动态高度,高度应基于文本长度和最大宽度 当此文本以单行形式出现,没有行分隔符时,就会出现问题。不管文本有多大,如果没有行分隔符,它会检测到文本适合一行,这样我的单元格高度就不会增加 我做错什么了吗?我怎样才能做到呢?谢谢Objective c sizeWithFont:和sizeWithAttributes:don';我一条线也干不了,objective-c,uitableview,cell,row-height,Objective C,Uitableview,Cell,Row Height,我正在尝试为表格中的单元格设置动态高度,高度应基于文本长度和最大宽度 当此文本以单行形式出现,没有行分隔符时,就会出现问题。不管文本有多大,如果没有行分隔符,它会检测到文本适合一行,这样我的单元格高度就不会增加 我做错什么了吗?我怎样才能做到呢?谢谢 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat cellheight = 3
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat cellheight = 35.0f; // BASE
NSString *text = @"...";
if (text) {
UIFont *font = (indexPath.row == 0) ? [UIFont systemFontOfSize:14] : [UIFont systemFontOfSize:12];
CGSize constraintSize = CGSizeMake(self.tableView.frame.size.width, CGFLOAT_MAX);
if (IS_EARLIER_THAN_IOS7) {
CGSize size = [text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByCharWrapping];
cellheight += size.height;
} else {
CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0f]}];
CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
cellheight += adjustedSize.height;
}
return (indexPath.row == 0) ? cellheight + 40.0f : cellheight;
}
}有一种更简单的方法可以做到这一点: 首先将文本设置为
ui标签
,并设置所有所需的字体、大小。然后调用标签上的sizehattfits
方法
CGSize size size=[itemLabel-sizehatfits:CGSizeMake(itemlabelwidth,CGFLOAT_MAX)]代码>
在调用sizeThatFits:
之前,也不要忘记设置numberOfLines
和lineBreakMode
itemLabel.numberOfLines=0;
itemLabel.lineBreakMode=NSLineBreakByWordWrapping;
注意1:调用sizeThatFits
不会将新帧设置为UILabel,它只是计算并返回新帧。然后,必须通过添加x和y原点值将框架设置为标签。这就变成了:
CGSize sizze =[itemLabel sizeThatFits:CGSizeMake(itemNameLabelWidth, CGFLOAT_MAX)];
CGRect namelabelFrame = itemLabel.frame;
namelabelFrame.size = sizze;
itemLabel.frame = namelabelFrame;
注2:此代码在单元格中没有问题,但在计算单元格中的高度时,您可能需要稍微优化此代码。由于没有可使用的单元格,您可以初始化UILabel
对象,并对其执行此代码以估计高度。但是在heightforrowatinexpath:
中进行UIView
初始化不是一个好主意,因为它们会在滚动时显著影响性能
因此,您要做的是将一个已初始化(且应用了所有格式)的UILabel作为类变量,并将其重新用于高度计算。有一种更简单的方法:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:") __TVOS_PROHIBITED; // NSTextAlignment is not needed to determine size
首先将文本设置为ui标签
,并设置所有所需的字体、大小。然后调用标签上的sizehattfits
方法
CGSize size size=[itemLabel-sizehatfits:CGSizeMake(itemlabelwidth,CGFLOAT_MAX)]代码>
在调用sizeThatFits:
之前,也不要忘记设置numberOfLines
和lineBreakMode
itemLabel.numberOfLines=0;
itemLabel.lineBreakMode=NSLineBreakByWordWrapping;
注意1:调用sizeThatFits
不会将新帧设置为UILabel,它只是计算并返回新帧。然后,必须通过添加x和y原点值将框架设置为标签。这就变成了:
CGSize sizze =[itemLabel sizeThatFits:CGSizeMake(itemNameLabelWidth, CGFLOAT_MAX)];
CGRect namelabelFrame = itemLabel.frame;
namelabelFrame.size = sizze;
itemLabel.frame = namelabelFrame;
注2:此代码在单元格中没有问题,但在计算单元格中的高度时,您可能需要稍微优化此代码。由于没有可使用的单元格,您可以初始化UILabel
对象,并对其执行此代码以估计高度。但是在heightforrowatinexpath:
中进行UIView
初始化不是一个好主意,因为它们会在滚动时显著影响性能
因此,您要做的是将一个已经初始化(并且应用了所有格式)的UILabel作为类变量,并将其重新用于高度计算
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:") __TVOS_PROHIBITED; // NSTextAlignment is not needed to determine size
您应该使用“boundingRectWithSize:options:attributes:context:”而不是“sizeWithAttributes:”
这是一个样本
CGSize size = [text boundingRectWithSize:CGSizeMake(_myTableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
您应该使用“boundingRectWithSize:options:attributes:context:”而不是“sizeWithAttributes:”
这是一个样本
CGSize size = [text boundingRectWithSize:CGSizeMake(_myTableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
对于计算UILabel的大小,请使用sizeThatFits:
比从NSString的boundingRectWithSize:
计算更理想的大小。对于计算UILabel的大小,请使用sizeThatFits:
比从NSString的boundingRectWithSize:
计算更理想的大小。