0

I have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class. Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.

I have the following in viewDidLoad of the tableview controller:

tableView.estimatedRowHeight = 56.0 
tableView.rowHeight = UITableViewAutomaticDimension

Any idea why this isn't working?

4thSpace
  • 43,672
  • 97
  • 296
  • 475

2 Answers2

2

Try my answer its work perfectly.

var record : NSArray = NSArray()
var  hight: CGFloat = 0.0

Put this code in viewDidLoad()

record = ["I have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class." ,"Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.","I have the following in viewDidLoad of the tableview controller:"," have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class.","Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.","I have the following in viewDidLoad of the tableview controller:","i just give you one link at put place i use label and you can now use your textview and give same constrain that i give in that link and try it so your problem will be solve","I have the following in viewDidLoad of the tableview controller:"];

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return record.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Textviewcell", forIndexPath: indexPath)

    let textview: UITextView = (cell.viewWithTag(5) as! UITextView)

    textview.text = record.objectAtIndex(indexPath.row) as? String

    return cell

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    // 7.1>
   hight = self.findHeightForText(self.record.objectAtIndex(indexPath.row) as! String, havingWidth: self.view.frame.size.width - 10, andFont: UIFont.systemFontOfSize(14.0)).height
   return 44 + hight
}



func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {

    var size = CGSizeZero
    if text.isEmpty == false {
        let frame = text.boundingRectWithSize(CGSizeMake(widthValue, CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
        size = CGSizeMake(frame.size.width, ceil(frame.size.height))
        }
        return size
}

In Swift 3.0

func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {

        var size = CGSize.zero 
        if text.isEmpty == false {
            let frame = text.boundingRect(with: CGSize(width: widthValue, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
            size = CGSize(width: frame.size.width, height: ceil(frame.size.height))//CGSizeMake(frame.size.width, ceil(frame.size.height))
        }
        return size
    }

Here are some screen shot .Storyboard

Runtime tableview with UITextView

Jitendra Modi
  • 2,344
  • 12
  • 34
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
-2

You should use the delegate method

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

to set the height for the row. Then, inside that method, return the height that the cell should be by calculating the height of the textview which will occupy the space. You should use the function:

-(CGFloat)heightForTextViewRectWithWidth:(CGFloat)width andText:(NSString *)text withBuffer:(float)buffer
{
    UIFont * font = [UIFont fontWithName:@"YourFontName" size:15.0f]; // Replace with your font name and size
    NSDictionary *attributes = @{NSFontAttributeName: font};

    // this returns us the size of the text for a rect but assumes 0, 0 origin, width is the width of that your text box will occupy. 
    // Ex. If you text box has padding of 12 both trailing and leading to the cell, then width should be the cell's width minus 24.
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                 options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                              attributes:attributes
                                 context:nil];

    // return height of rect
    return rect.size.height;
}

This will determine the rect that your text view will occupy in the cell. The height that is returned is equal to the height of the text view, so if you want the cell to be taller than the text box, add that padding.

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

    NSString * yourText = commentDict.text; // or however you are getting the text
    float widthPadding = 24.0f;
    float heightPadding = 16.0f;
    float height = [self heightForTextViewRectWithWidth: (tableView.frame.size.width - widthPadding) andText:yourText];
    return height + heightPadding;

}

Hope this helped!

Boryk
  • 36
  • 9