简体   繁体   English

如何获取 UITextField 的行高

[英]How do I get the line height of UITextField

I have a few questions that I can't find the answers online:我有几个问题在网上找不到答案:

1.How do I get the line height of UITextField? 1.如何获取UITextField的行高?

2.How do I get UITextField to only enter a maximum of 3 lines of text? 2.如何让 UITextField 最多只能输入 3 行文本?

3.How do I get UITextField to only enter a maximum of 25 characters per line? 3.如何让 UITextField 每行最多只能输入 25 个字符?

and thanks for answer!并感谢您的回答!

As the comment says, you should use UITextView, not UITextField.正如评论所说,您应该使用 UITextView,而不是 UITextField。 UITextField handles only a single line. UITextField 只处理一行。 Use UITextView to process multiple lines of text.使用 UITextView 处理多行文本。

I'm going to give you an opinion on number 3.我要给你一个关于第 3 条的意见。

First, there is something that limits the length of the textview.首先,有一些东西limits the length了 textview limits the length
Auto-layout to a length of 25 characters to fit the font size you want.自动布局到 25 个字符的长度以适合您想要的字体大小。

The second is that when the text reaches 25 characters, it adds a line-breaking symbol .第二个是当文本达到25个字符时, 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.如果您输入的not backspace ,它将被执行。 If it's 25 characters, you can add the line symbol '\\n' to the text in textView and change it.如果是25个字符,你可以在textView中的文本中添加行符号'\\n'并更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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