0

I have a few questions that I can't find the answers online:

1.How do I get the line height of UITextField?

2.How do I get UITextField to only enter a maximum of 3 lines of text?

3.How do I get UITextField to only enter a maximum of 25 characters per line?

and thanks for answer!

  • 1
    First, "line height" is a font property, not a text field property. Second, `UITextField` is only a single line... `UITextView` is for multi-line text input. Third, do you mean max of 3 lines ***when the text wraps***? Or, 3 lines meaning max of 2 **Return** / **Newline** entries? Fourth, do you mean 25 characters per line and then ***wrap*** the text? Fifth, have you tried searching for any of these questions? Please review [ask]. – DonMag Jul 21 '21 at 14:26

1 Answers1

1

As the comment says, you should use UITextView, not UITextField. UITextField handles only a single line. Use UITextView to process multiple lines of text.

I'm going to give you an opinion on number 3.

First, there is something that limits the length of the textview.
Auto-layout to a length of 25 characters to fit the font size you want.

The second is that when the text reaches 25 characters, it adds a line-breaking symbol.


extension mainVC: UITextViewDelegate {

   func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        if let char = text.cString(using: String.Encoding.utf8) {
            let isBackSpace = strcmp(char, "\\b")
            
            if isBackSpace != -92 {
                if textView.text.count == 25 {
                    textView.text += "\n"
                }
            }
        }
        
     
        return true
    }
}

If what you entered is not backspace, it will be executed. If it's 25 characters, you can add the line symbol '\n' to the text in textView and change it.

Hailey
  • 302
  • 1
  • 7