ios - 如何使 UITableView 行高响应用户的首选文本大小(动态类型)?

标签 ios uitableview ios7 textkit

我希望 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/

相关文章:

ios - 导航 UIBarButtonItem 在设备上的外观不同

ios - swift 在其他 View Controller 中启动计时器

iphone - 自定义 UITableViewCell,其样式看起来非常像 UITableViewCellStyleValue2

ios - scichart中的x轴位置

ios - 通过应用程序委托(delegate)从 TableView Controller 更改单元格内的值

ios - UITableView reloadData 在 iOS 中不起作用

ios - 调暗 iOS7 状态栏

css - iOS 7 iPad Safari Landscape innerHeight/outerHeight 布局问题

ios - ios7 : CGContextDrawImage or UIView setImage? 速度大幅下降

ios - 在 StoryBoard 中显示图像而不是 UIBarButton 的文本