[英]UITableViewCell/UITableView: Dynamic resizing of 2 UILabels in UITableViewCell is flickering
我想在图像中显示类似这样的内容,其中两个UILabel垂直于另一个基于文本展开。
这就是我尝试这样做的方式。
对于label1,我正在使用NSMutableAttributedString,以便可以设置行距。 Label2具有普通的String文本。
UITableView的属性
tableView.estimatedRowHeight = 110
tableView.rowHeight = UITableViewAutomaticDimension
heightForRowAtIndexPath()未实现,因为这些属性将动态处理高度
在CellForRowAtIndexPath()中
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 15
paragraphStyle.alignment = NSTextAlignment.Right
attrString = NSMutableAttributedString(string: "HUGE TEXT 1")
attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
myCell.label1.attributedText = attrString
myCell.label2.text = "HUGE TEXT 2"
问题:向下滚动(向下的新单元格)没有问题,但是当我向上滚动(向上的新单元格)时,单元格开始闪烁,然后调整大小以适合滚动的结尾。 闪烁仅在UILabel2上。 对于这种布局,我似乎没有在堆栈溢出中找到任何解决方案。
这可以帮助您,在UITableViewCell中实现此方法:
+ (CGFloat) cellHeightForContent:(NSString)aContent {
CGFloat result = 0;
CGSize textDims = [aContent getCorrectSizeWithFont:[UIFont fontWithName:@"Helvetica" size:13] constrainedToSize:CGSizeMake(kLabelWidth, CGFLOAT_MAX)];
result = 2 * kMarginHeight + textDims.height;
return result;
}
此方法返回的值将在heightForRowAtIndexPath
方法中使用。
之后,您需要在NSString类别中定义方法getCorrectSizeWithFont
,如下所示:
- (CGSize)getCorrectSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[attrsDictionary setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
NSMutableAttributedString* attrStrWithLinks = [[NSMutableAttributedString alloc] initWithString:self attributes:attrsDictionary];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrStrWithLinks);
CGSize sz = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0,0),NULL,CGSizeMake(size.width,CGFLOAT_MAX),NULL);
if (framesetter) CFRelease(framesetter);
return CGSizeMake(sz.width,sz.height);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.