Dem*_*rpl 8 objective-c uitableview ios ios7
有很多解决方案使用"sizeWithFont"或类似的东西,从iOS 7开始就被弃用了.
这是我到目前为止拼凑的一些代码.高度变化,但完全没有变化:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = "The Cell's Text!";
cell.textLabel.numberOfLines = 0;
[cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
[cell.textLabel sizeToFit];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBounds.size;
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:"The Cell's Text!"];
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:aString];
CGSize size = [calculationView sizeThatFits:CGSizeMake(screenSize.width, FLT_MAX)];
return size.height;
}
Run Code Online (Sandbox Code Playgroud)
再举一个例子,这里有一个类似的答案:https://stackoverflow.com/a/9828777/693121虽然我之前说过,但它使用了弃用的代码.
rde*_*mar 14
您应该使用文档中提到的方法来替换旧的 - boundingRectWithSize:options:attributes:context:.这是一个我认为应该工作的例子(无论如何它适用于多行标签).
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSStringDrawingContext *ctx = [NSStringDrawingContext new];
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:@"The Cell's Text!"];
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:aString];
CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:calculationView.font} context:ctx];
return textRect.size.height;
}
Run Code Online (Sandbox Code Playgroud)
这假定您希望文本视图的大小为self.view.如果没有,您应该使用initWithFrame作为文本视图,并为boundingRectWithSize:参数传递calculateView.frame.size.
这是最简单的版本,iOS 7完成所有繁重的工作(仅适用于基于autolayout的uitableviewcells):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Cell"];
[self configureCell:cell];
// edit: Need to call [cell layoutIfNeeded]; otherwise the layout changes wont be applied to the cell
[cell layoutIfNeeded];
return [cell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height;
}
Run Code Online (Sandbox Code Playgroud)
systemLayoutSizeFittingSize重新评估自动布局约束并返回单元格的完美高度.容易,对吗?
小智 -1
要获取字符串的大小,您应该使用- (CGSize)sizeWithAttributes:(NSDictionary *)attars.
因此,对于您的示例,您将计算文本标签的高度,如下所示:
CGSize *cellSize = [cell.textLabel.text sizeWithAttributes:@{NSFontAttributeName:[cell.textLabel.font]}];
Run Code Online (Sandbox Code Playgroud)
或者
CGSize *rowSize = [aString sizeWithAttributes:nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19240 次 |
| 最近记录: |