Shubham Ojha
Shubham Ojha

Reputation: 471

How to stop a UITextField's attributed placeholder text from getting cut off on iOS 13?

I am creating a custom textfield for search bar purpose. I need custom font and font color. Though its working correctly on iOS 11-12.* but on iOS 13 its frame becomes small and thus the placeholder truncates. FYI I am using attributedPlaceholder for this. How can I stop this?

class SearchBar: UITextField {
    
    @IBInspectable var placeholderText : String = "" {
        didSet {
            self.attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.appFont(size: 14.0)])
        }
    }

enter image description here

Upvotes: 1

Views: 636

Answers (2)

Shubham Ojha
Shubham Ojha

Reputation: 471

After a lot of headache I figured out the solution.

Solution: I was adding leftView and rightView to maintain the padding. Instead of adding a view to the rightView of the textfield you need to add a button. Don't know why it works but it works. If someone has more to add you are welcome.

Upvotes: 1

Vahe Aslanyan
Vahe Aslanyan

Reputation: 126

One solution would be to add an empty view inside your textField as its leftView property.

Wherever you set up your textField, say in your view controller in viewDidLoad, you can add the following:

        //Create an empty padding view with width and height that you want
        let emptyPaddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20))
   
        //Add the view to your text field leftView property and set leftViewMode to .always
        yourTextField.leftView = emptyPaddingView
        yourTextField.leftViewMode = .always

Upvotes: 0

Related Questions