UITextView in Cell Swift

I have been messing with these constraints for hours and cannot figure this out.
I need to have Dynamic cell height with my text view.

enter image description here
enter image description here

  • Get X and Y coordinates of a word in UITextView
  • UITextView's text going beyond bounds
  • Find the number of characters that fits in a UITextView with constant width and height with a given string?
  • Getting Data From all TableViewCells
  • Why isn't is possible to append a String! via += operator?
  • Show more button next to end of text Swift
  • As you can see its overlapping my time stamp. As of right now I have zero(no) constraints on the time stamp. I have tried every combination possible to make this work and I cant get it.

    Also am using

       override func viewDidLoad() {
            super.viewDidLoad()
    
    
    
            //TableView Cell word wrap (Dynamic Text)
            self.tableView.dataSource = self
            self.tableView.delegate = self
            self.tableView.estimatedRowHeight = 78
            self.tableView.rowHeight = UITableViewAutomaticDimension
    

    3 Solutions Collect From Internet About “UITextView in Cell Swift”

    For Swift 3.1, 4.0

    It will be achieved by setting constraints and UITableViewAutomaticDimensions.

    Step 1:

    enter image description here

    Step 2:

    enter image description here

    Step 3:

    enter image description here

    Step 4:

    Remove Scrolling for UITextView

    enter image description here

    Step 5:

    Add Below code,

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    
        return UITableViewAutomaticDimension
    }
    

    Note:

    If you have UILabel then, Replace UITextView to UILabel from above solution and label.numberOfLines = 0.

    enter image description here

    Output:

    enter image description here

    If your cell height is dependent on text size then follow this: Add the method in your ViewController.swift

        func calculateHeight(inString:String) -> CGFloat {
             let messageString = inString
             let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 15.0)]
    
         let attributedString : NSAttributedString = NSAttributedString(string: messageString, attributes: attributes)
    
         let rect : CGRect = attributedString.boundingRect(with: CGSize(width: 222.0, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)
    
          let requredSize:CGRect = rect
          return requredSize.height
    }
    

    Set the width of your text label Then call this function:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                heightOfRow = self.calculateHeight(inString: conversations[indexPath.row].description)
    
                return (heightOfRow + 60.0)
        }
    

    For Basic cell:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
               return UITableViewAutomaticDimension
        }
    

    This function will not work for custom cells.

    If your cell height is dependent on image height, then return image height + someFloatValueAccordingToYourChoice.

    Hope it will work.

    User vertical spacing between the timeStamp and the message, and set the constant to >=2