简体   繁体   中英

How to show the full placeholder text in an ios UITextfield?

The placeholder text does show the triple points to cut off the text.

The text doesn't even take half of the textfield width, and the three points are longer than the text. Is there a way to go inside the field and adjust the threshold to avoid the cutoff?

I do know the programatic way, and use that for the moment, to resize the placeholder tekst, till it fits.

    let labelKeyPath: String = "_placeholderLabel"
    var label : UILabel = self.Distance.value(forKeyPath: labelKeyPath) as! UILabel
    label.adjustsFontSizeToFitWidth = true
    label = self.FocalLength.value(forKeyPath: labelKeyPath) as! UILabel
    label.adjustsFontSizeToFitWidth = true

Herby I do add some images of the current IB settings and results. Please keep adding points to let me show the current and future images embedded, thank you.

The IB view, settings and app result

或者,您可以创建自己的UITF子类并重写:

open func placeholderRect(forBounds bounds: CGRect) -> CGRect

You can customize the textfield to fix you issue. put this CustomTextField in your Storyboard textfield class type. here am setting minimum scaling factor to 0.3. that helps to fix the ...

class CustomTextField: UITextField {
    override func layoutSubviews() {
        super.layoutSubviews()

        for subview in subviews {
            if let label = subview as? UILabel {
                label.minimumScaleFactor = 0.3
                label.adjustsFontSizeToFitWidth = true
            }
        }
    }
} 

You should change Autolayouts Constraints with leading , center vertically with Distance and horizontal space with the Distance.

You can also wrap up text with resize the textfield using minimum font size .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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