1

I want that if user add text and it add new line it should increase height and if user delete soometext and some line than it should decrease its height

For this I'have tried below solution

func textViewDidChange(_ textView: UITextView) {
        self.updateCharacterCount()
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4), 
    execute: {
        let pos = textView.endOfDocument
        self.referenceOfClass?.textOfTextViewForTitle = 
        self.title_Name.text
        let currentRect = textView.caretRect(for: pos ?? 
        UITextPosition.init())

        if (currentRect.origin.y == -1 || currentRect.origin.y == 
        CGFloat.infinity){
         if let val = self.indexPath{
             self.referenceOfClass?.previousRectforTitle = currentRect
             self.referenceOfClass?.inq_Tableview.reloadRows(at: 
              [val], with: .automatic)
         }
     }
     else if currentRect.origin.y < 
         self.referenceOfClass?.previousRectforTitle.origin.y ?? 0.0 {
         if let val = self.indexPath{
             self.referenceOfClass?.previousRectforTitle = currentRect
             self.referenceOfClass?.inq_Tableview.reloadRows(at: 
              [val], with: .automatic)
        }
    }
    })
}

but it is not working in this textView.caretRect(for: pos ?? UITextPosition.init()) always return same value (0,-1)

So how can I solve this?

I am using xcode10.1 and this textview is in tableviewcell

  • Check this link: https://stackoverflow.com/questions/18368567/uitableviewcell-with-uitextview-height-in-ios-7/18818036#18818036 – Mahak Mittal Jan 29 '19 at 08:21

2 Answers2

0

You can try this, Take Outlet of hight constraint and code below

@IBOutlet weak var txtMessageHeightConstraint: NSLayoutConstraint!

extension GigMessageSocketViewController:UITextViewDelegate
{

    func textViewDidChange(_ textView: UITextView)
    {
        isClickOnSendMessage = true
        let contentSize = txtMessage.sizeThatFits(CGSize(width: txtMessage.bounds.size.width, height: CGFloat(Float.greatestFiniteMagnitude)))

        if textView.contentSize.height < 100.0
        {
            txtMessageHeightConstraint.constant = CGFloat(ceilf(Float(contentSize.height)))
            textView.layoutIfNeeded()
            textView.updateConstraints()
            textView.scrollRangeToVisible(textView.selectedRange)
        }
    }
}    
0

My first thought on how to solve this issue would be to create a custom UITextfield that it would be a UIView with content mode scaleToFill that inside a UILabel and you would make every component of the UITextfield clear

self.textColor = UIColor.clear
self.tintColor = UIColor.clear
self.borderStyle = UITextBorderStyle.none
self.placeholder=""

and on the function

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}

You would update the UILabel text so the scaling would be automated because of the contraints.

Vasilis D.
  • 1,416
  • 13
  • 21