UITextView in Cell Swift

后端 未结 3 372
滥情空心
滥情空心 2021-01-07 12:56

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.

As you can see its o

3条回答
  •  时光说笑
    2021-01-07 13:43

    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.

提交回复
热议问题