UITableViewCell调整大小以适合详细文本

清除

我正在尝试调整a的大小UITableViewCell以在选择时显示/隐藏详细信息的一部分。所有单元格都显示详细信息的第一行。当用户点击一个单元格时,应该显示整个详细信息,而不移动文本的当前可见部分。

目前,我做了以下情况:
cellForRowAtIndexPath:

cell.textLabel.text = ...;
cell.detailTextLabel.text = ...;

if ([indexPath isEqual:self.currentSelection])
{
    cell.detailTextLabel.numberOfLines = CGFLOAT_MAX;
}
else
{
    cell.detailTextLabel.numberOfLines = 1;
}

didSelectRowAtIndexPath:

if ([self.currentSelection isEqual:indexPath])
{
    self.currentSelection = nil;
}
else
{
    self.currentSelection = indexPath;
}

[tableView reloadData];

而在 heightForRowAtIndexPath

float minHeight = 60;

if ([indexPath isEqual:self.currentSelection])
{
    KPLexiconEntry *lexiconEntry = ...;

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;

    NSDictionary * attributes = @{NSFontAttributeName : cellFont, NSParagraphStyleAttributeName : paragraphStyle};
    CGSize labelSize = [lexiconEntry.explanation boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;

    float height = labelSize.height + 44;
    return height > minHeight ? height : minHeight;
}

return minHeight;

这会按照我的意愿显示或隐藏详细信息文本,但是单元格内的文本(包括textLabel.text)会向上或向下跳跃几个像素。
如何防止这种情况并使外观光滑?

清除

我想出了一种解决方法:我创建了和使用
而不是使用属性字符串仅以比其他字体(以前是ind 更大的字体显示标题(以前)。我创建了一个方法来创建属性字符串:detailLabelNSAttributedStringtextLabel.attributedTexttextLabeldetailLabel

- (NSAttributedString*)attributedTextForIndexPath:(NSIndexPath*)indexPath {
    id itemForIndexPath = ...;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];

    UIFont *titleFont = [UIFont systemFontOfSize:16];
    NSDictionary *titleAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: titleFont};
    NSString *titleRaw = [NSString stringWithFormat:@"%@\n", @"itemForIndexPath.title"];
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:titleRaw attributes:titleAttributes];
    [attrString appendAttributedString:title];

    UIFont *textFont = [UIFont systemFontOfSize:14];
    NSDictionary *textAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: textFont};;
    NSString *textRaw = itemForIndexPath.text;
    if (![indexPath isEqual:self.currentSelection] && textRaw.length >= 30)
    {
        textRaw = [NSString stringWithFormat:@"%@...", [textRaw substringToIndex:27]];
    }

    NSAttributedString *text = [[NSAttributedString alloc] initWithString:textRaw attributes:textAttributes];
    [attrString appendAttributedString:text];

    return attrString;
}

如您所见,我手动限制了除所选单元格以外的所有单元格的文本长度。

然后,在中cellForRowAtIndexPath,我将其分配为:

cell.textLabel.attributedText = [self attributedTextForIndexPath:indexPath];

并在heightForRowAtIndexPath

NSAttributedString *text = [self attributedTextForIndexPath:indexPath];
CGSize constraintSize = CGSizeMake(255, CGFLOAT_MAX);
CGSize size = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
return ceil(size.height + 22); // adding 22 for padding 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

UITableViewCell调整大小以适合详细文本

来自分类Dev

如何调整UITableViewCell的大小以适合其内容?

来自分类Dev

调整UITableViewCell的大小以适合不同的屏幕尺寸

来自分类Dev

调整文本框大小以适合文本

来自分类Dev

在Swift中调整文本大小以适合标签

来自分类Dev

动态调整容器大小以适合文本

来自分类Dev

如何使UILabel调整大小以适合文本

来自分类Dev

如何使textarea自动水平调整大小以适合输入的文本?

来自分类Dev

如何调整按钮大小以适合Delphi FireMonkey中的文本?

来自分类Dev

调整导航栏标题字体大小以适合文本

来自分类Dev

如何调整文本(字体)的大小以适合UISegmentedControl的UISegment?

来自分类Dev

调整文本大小以适合浏览器中的可用空间

来自分类Dev

调整文本大小以适合DIV-精度更高吗?

来自分类Dev

调整IText pdf的大小,使文本适合一页

来自分类Dev

调整列大小时适合文本字段的宽度

来自分类Dev

调整文本区域的大小以垂直适合页面

来自分类Dev

CSS避免容器调整大小以适合粗体文本

来自分类Dev

调整UITableViewCell高度的大小

来自分类Dev

调整大小的 UITableViewCell 变小

来自分类Dev

调整动态文本大小,以使整个文本行适合文本字段

来自分类Dev

使文本大小适合textview

来自分类Dev

调整图像大小以适合JLabel

来自分类Dev

调整大小以适合div的图标

来自分类Dev

调整元素大小以适合容器

来自分类Dev

调整JFrame的大小以适合JPanel

来自分类Dev

调整大小以适合div的图标

来自分类Dev

调整图形大小以适合 PictureBox

来自分类Dev

调整 UICollectionViewCell 的大小以适合图像

来自分类Dev

无法将文本区域调整为适合Windows大小的大小

Related 相关文章

热门标签

归档