自动布局。基于UITextview文本的UITableView动态高度
我已经找到了很多如何解决这个问题的链接,但如果字符串中有不同数量的字母,效果就不同了 所以我使用的第一种方法是:自动布局。基于UITextview文本的UITableView动态高度,uitableview,ios7,ios6,autolayout,ios-autolayout,Uitableview,Ios7,Ios6,Autolayout,Ios Autolayout,我已经找到了很多如何解决这个问题的链接,但如果字符串中有不同数量的字母,效果就不同了 所以我使用的第一种方法是: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *comment = [self.comments objectAtIndex:indexPath.row]; NSString *text =
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *comment = [self.comments objectAtIndex:indexPath.row];
NSString *text = comment[@"text"];
CGSize stringSize = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
return stringSize.height;
}
我使用的第二种方法是:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *comment = [self.comments objectAtIndex:indexPath.row];
NSString *text = comment[@"text"];
UITextView *tempTV = [[UITextView alloc] init];
[tempTV setText:text];
CGFloat width = tableView.frame.size.width;
CGSize size = [tempTV sizeThatFits:CGSizeMake(width, MAXFLOAT)];
return size.height;
}
它可以工作,但有时我看不到最后一句话或最后一行如果文本长度超过5或6行,我还使用自动布局,使我的文本视图适合内容视图的所有方面
我还想支持iOS 6,似乎不适合我,我想我需要通过代码保持计算高度
我在调试代码时注意到,我的UITextView框架与情节提要大小相等。因此,宽度是600磅而不是显示的宽度320磅,我已经在-cellForRowAtIndexPath
方法中对其进行了调试。但是,即使我硬编码这个值,高度计算在任何情况下都会出现问题