Resizing UITextView in custom UITableViewCell

后端 未结 6 1465
天命终不由人
天命终不由人 2021-01-19 23:55

I have a custom UITableViewCell and I\'m trying to resize the UITextView inside it based on the content size. I\'m on iOS7 and using Autolayout.

6条回答
  •  粉色の甜心
    2021-01-20 00:03

    You have to do this calculation in

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    method and also resize the cell height accordingly.

    If you got it, it's okay. Or If you need the code sample, just ask again. I think you got it !

    Updated

        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
                UITextView *tempTV = [[UITextView alloc] init];
                [tempTV setText:@"your text"];
                CGFloat width = self.tableView.frame.size.width - TEXT_ORIGIN_X - TEXT_END_X;
                CGSize size = [tempTV sizeThatFits:CGSizeMake(width, MAX_HEIGHT)];
                return (TEXT_ORIGIN_Y + size.height + TEXT_BOTTOM_SPACE);
        }
    

提交回复
热议问题