简体   繁体   English

如何通过文本内容获取三个UITextViews高度以及如何通过总共3个TextViews高度更改表视图单元格的高度

[英]How to get the three UITextViews height by text content and change table view cell height by total 3 textViews height ios

I have custom cell with 3 textViews: 我有3个textViews的自定义单元格:

在此输入图像描述

I want to calculate the textview height of 3 textView and change tableView cell height dynamically and change height of textViews I am using this function to get the heigth for textViews 我想计算3 textView的textview高度并动态更改tableView单元格高度并更改textViews的高度我正在使用此函数来获取textViews的高度

cell1.textValue1=[self calculateHeightForText:cell1.textView1.text indexPath:indexPath];
cell1.textValue2=[self calculateHeightForText:cell1.textView2.text indexPath:indexPath];
cell1.textValue3=[self calculateHeightForText:cell1.textView3.text indexPath:indexPath];



-(CGFloat)calculateHeightForText:(NSString*)text indexPath:(NSIndexPath*)indexPath
{  

UITextView *calculationView = [textViews objectForKey:indexPath];
CGFloat textViewWidth = calculationView.frame.size.width;
if (!calculationView.attributedText) {
    // This will be needed on load, when the text view is not inited yet

    calculationView = [[UITextView alloc] init];
    calculationView.attributedText =[[NSMutableAttributedString alloc] initWithString:text];
    textViewWidth = 80;

     }

you should use autolayout constraints with unbreakable constraints or if you don't want to use autolayout constraints, then you calculate text height then manages all frames. 你应该使用具有不可破坏约束的自动布局约束,或者如果你不想使用自动布局约束,那么你计算文本高度然后管理所有帧。

pragma mark-heightCalculate 实用标记高度

-(CGFloat)heightCalculate:(NSString *)calculateText { - (CGFloat)heightCalculate:(NSString *)calculateText {

NSString *text = calculateText;

UIFont *font= [UIFont systemFontOfSize:14];

CGSize constraint = CGSizeMake(yourtestfieldWidth, FLT_MAX);

CGRect frame = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin
        attributes:@{NSFontAttributeName:font} context:nil];

CGSize size = CGSizeMake(frame.size.width, frame.size.height);

CGFloat height_lbl = size.height;

return (height_lbl);

} }

Try to add these methods 尝试添加这些方法

-(CGFloat)tableView:(UITableView *)tableView 
    heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension; }

-(CGFloat)tableView:(UITableView *)tableView
    estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

If you have set number of lines in the UICell to 0 in interfacebuilder, the heights should be automatically adjusted to fit your label.You might have to put all three labels into one, to make this work. 如果在interfacebuilder中将UICell中的行数设置为0,则高度应自动调整以适合您的标签。您可能必须将所有三个标签放在一起,才能完成此工作。

EDIT: If you have allready three strings in three labels, you could rewrite as follows: 编辑:如果您已经在三个标签中的所有三个字符串,您可以重写如下:

 NSString*string1 =@"myfirstline\n";
    NSString*string2 =@"mysecondline\n";
    NSString*string3 =@"mythirdline\n";
    NSString*preCellString= [string1 stringByAppendingString:string2];
    NSString*CellString = [preCellString stringByAppendingString:string3];
    cell.textLabel.text = CellString;

The height of the tableviewcells should be automatically adjustet to fit your strings. tableviewcells的高度应该自动调整以适应你的字符串。

If you are using autolayout, then you should use below method with unbreakable constraints, not need to use height method. 如果您使用的是自动布局,则应使用具有牢不可破约束的以下方法,而无需使用height方法。

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { func tableView(tableView:UITableView,estimateHeightForRowAtIndexPath indexPath:NSIndexPath)-> CGFloat {

    tableView.rowHeight = UITableViewAutomaticDimension

    return 85.0
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM