【问题标题】:Table cell height by UITextView where content alignment is NSTextAlignmentJustifiedUITextView 的表格单元格高度,其中内容对齐方式为 NSTextAlignmentJustified
【发布时间】:2012-12-07 05:21:30
【问题描述】:

我有一个 UITextView,其中内容由 NSTextAlignmentJustified 对齐。在 cellForRowAtIndexPath: 中将此文本视图添加到单元格内容视图并根据其内容更改框架

UITextView *commentsTextView = (UITextView *)[self getCommentField:comment];
[cell.contentView addSubview:commentsTextView];
CGRect tempFrame = commentsTextView.frame;
tempFrame.size.height = commentsTextView.contentSize.height;
commentsTextView.frame = tempFrame;

到目前为止,一切都很好。现在,如何为 heightForRowAtIndexPath: 检测该视图的 contentSize? 我试过了

NSString *comment = [[self.allComments objectAtIndex:indexPath.row] objectForKey:@"commentText"];
CGSize constraintSize = CGSizeMake(315.0f, MAXFLOAT);
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14.0]
                  constrainedToSize:constraintSize
                      lineBreakMode:NSTextAlignmentJustified];

我也试过 lineBreakMode:NSLineBreakByWordWrapping 等等。但是当文本很长时,单元格高度总是更小,因为似乎没有办法计算 justified 文本的大小(因为文本中间有空格)。字体相同。

【问题讨论】:

    标签: ios cocoa-touch uitableview ios6 uitextview


    【解决方案1】:

    好的,我明白了。 UITextView 每边有 8 个像素的昆虫,我的 CGSizeMake(315.0f, MAXFLOAT); 必须是 CGSizeMake(299.0f, MAXFLOAT);

    【讨论】: