我正在尝试调整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 )更大的字体显示标题(以前是)。我创建了一个方法来创建属性字符串:detailLabel
NSAttributedString
textLabel.attributedText
textLabel
detailLabel
- (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] 删除。
我来说两句