2015-05-15 63 views
0

UITableViewCell with two Dynamic UILabels的UITableViewCell/UITableView的:在UITableViewCell中2个UILabels动态调整闪烁

我要在图像中显示一些像这样的事情在两个UILabels垂直旁边基于文本的另一个扩大。

这就是我试图做的。

对于label1,我使用NSMutableAttributedString,以便我可以设置行间距。 Label2与正常的字符串文本。 )的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" 

问题:当向下滚动(来自下方的新细胞)没有问题,但是当我向上滚动时(新细胞从上向下),细胞开始闪烁,并且t母鸡调整大小以适应卷轴的末尾。闪烁仅在UILabel2上。对于这种布局,我似乎没有发现堆栈溢出的解决方案。

回答

0

这可能会帮助你,在你的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方法使用。

之后,你需要定义方法getCorrectSizeWithFont在一个NSString类别,这样的:

- (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); 
}