简体   繁体   中英

UITextview height in UITableview swift

Im facing a problem while setting up UITextview in UITableViewCell . As the function heightforrowatindexpath is running before cellforrowatindexpath so I cannot calculate the size of content of UITextview .

Im calculating the size by simply textview.contentSize.height in cellforrowatindexpath function.

Im basically want UITextview contentHeight fit in UITableViewCell so no scroll is visible for every UITextView and it will show full content by increasing the height of UITableViewCell .

I also a UILabel just above the UITextView in UITableViewCell and I tried to set contraints for both but unable to achieve my goal.

How can I calculate the size of UITableViewCell dynamically in following function:

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

}

PS: Is it possible to use increase the UITableViewCell height a little bit according to device height, using auto layout or some technique else.

You can just pass your textView with the content in order to calculate the height of the textView

func calculateHeight(textView:UITextView, data:String) -> CGRect {

    var newFrame:CGRect! 

    // I was using the HTML content here. If your content is not HTML you can just pass the stringValue to your textView
    do {
        let attr = try NSAttributedString(data: html.dataUsingEncoding(NSUTF16StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
        textView.attributedText = attr

    }catch {
        //Handle Exceptions
    }

    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    print("height \(newFrame.height)")
    return newFrame
}

You can call this method in your heightForRowAtIndex so that in return you can get a new updated frame from which you can fetch the new updated height.

Make sure to keep scrollEnabled as false of your cell's textView in cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        //Cell making here......

        cell.yourTextView.scrollEnabled = false
        return cell  
    }
}

1) In heightForRowAtIndexPath give the cell size the following:

return UITableViewAutomaticDimension

2) ... and add the following:

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension }

I hope it helps ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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