我希望 UITableView
的行高能够响应用户首选文本大小的变化。例如,当首选文本大小增加时,我想按比例增加行高。这是我到目前为止所拥有的:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
// observe when user changes preferred text size
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
}
- (void)preferredContentSizeChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
// leverage Text Kit's Dynamic Type
cell.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
cell.textLabel.text = @"All work and no play...";
return cell;
}
那么计算反射(reflect)用户首选文本大小的行高的最佳方法是什么?
最佳答案
我最近完成了这个,发现它非常简单。而不是使用 sizeWithFont:
你应该使用新的 boundingRectWithSize:options:attributes:context
iOS 7 中的方法。
照常设置表格 View 单元格并指定 preferredFontForTextStyle:
在您的文本标签上:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier forIndexPath:indexPath];
//set your table view cell content here
[[cell textLabel] setText:@"Lorem ipsum dolour sit amet."];
[[cell textLabel] setNumberOfLines:0];
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
return cell;
}
然后要正确确定文本标签的大小,计算 boundingRectWithSize:options:attributes:context
计算所需的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//add your table view cell content here
NSString *string = @"Lorem ipsum dolor sit amet.";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]};
CGRect frame = [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(tableView.bounds), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
return ceilf(CGRectGetHeight(frame);
}
您可能还想对表格 View 单元格进行子类化以监听 UIContentSizeCategoryDidChangeNotification
当用户在 Settings.app 中更改他们的首选项时,您可以在此时更新您的 UI 的通知
- (void)contentSizeCategoryDidChangeNotificationHandler:(NSNotification *)notification
{
[[self textLabel] setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
}
如果您需要文本标签周围的额外填充,您可以定义一个常量值,例如
static CGFloat const TableViewCellPadding = 10.0;
有了这个,您可以将一个常量值添加到从 tableView:heightForRowAtIndexPath:
返回的值中
return (ceilf(CGRectGetHeight(frame) + TableViewCellPadding);
或者您可以插入从 boundingRectWithSize:options:attributes:context
返回的框架因此:
CGRect frame = [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(tableView.bounds), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
frame = CGRectInset(frame, 0.0, TableViewCellPadding);
关于ios - 如何使 UITableView 行高响应用户的首选文本大小(动态类型)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20115053/