Introduction: In UITextField, sometimes the content of placeHold can be very long (for example, English & French can be very long for multi-languages), and the content needs to be displayed completely, and the UI may not modify the design for this situation. So you need to take to the placeholdLabel, then put the laebl adjustsFontSizeToFitWidth set to true, then this method in (iOS, 13 *) doesn’t work, so I according to the length of placehold text, Adjust fontSize dynamically.

extension UITextField { /// set textField placehold and adjustsFontSizeToFitWidth . please make sure you have set this UITextField' s frame /// - Parameters: /// - placeholdStr: placehold string /// - textFont: placehold font /// - textColor: placehold color /// - adjustsFontSizeToFitWidth: need adjustsFontSizeToFitWidth open func placeholdAttrAndAdjustFontFitWidth(placeholdStr: String, textFont: UIFont, textColor: UIColor, adjustsFontSizeToFitWidth: Bool) { placeholdAttrAndAdjustFont(placeholdStr: placeholdStr, textFont: textFont, textColor: textColor, adjustsFontSizeToFitWidth: adjustsFontSizeToFitWidth, textFieldWid: self.width) } /// set textField placehold' attributeString and adjustsFontSizeToFitWidth . /// - Parameters: /// - placeholdStr: placehold string /// - textFont: placehold font /// - textColor: placehold color /// - adjustsFontSizeToFitWidth: need adjustsFontSizeToFitWidth /// - textFieldWid: aim width . If you use snapkit to set self's constraints , you should set this param open func placeholdAttrAndAdjustFont(placeholdStr: String, textFont: UIFont, textColor: UIColor, adjustsFontSizeToFitWidth: Bool, textFieldWid: CGFloat) {if #available(iOS 13.0, *) { let canUseWid = textFieldWid - 14*2 //left place for border guard canUseWid > 0 else { return } let attrStr = NSMutableAttributedString(string: placeholdStr, attributes: [NSAttributedString.Key.font : textFont]) let needWid = attrStr.getWidth(height: self.height > 0 ? Self. height: 12.0) + 14 //Without 14px,placehold content can't show Completly. I want know why too. CGFloat = textFont.pointSize if (needWid > canUseWid) && adjustsFontSizeToFitWidth { newFontSize = min(textFont.pointSize * (canUseWid/needWid), textFont.pointSize) } self.attributedPlaceholder = NSAttributedString(string: placeholdStr, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: newFontSize), NSAttributedString.Key.foregroundColor : textColor]) }else { self.attributedPlaceholder = NSAttributedString(string: placeholdStr, attributes: [NSAttributedString.Key.font : textFont, NSAttributedString.Key.foregroundColor : textColor]) guard let lab = self.value(forKey: "placeholderLabel") as? UILabel else { return } lab.adjustsFontSizeToFitWidth = adjustsFontSizeToFitWidth } } }

How to use it, called in viewDidLoad

func testAttrStr() { let textF = UITextField(frame: CGRect(x: 100, y: 250, width: 100, height: 60)) view.addSubview(textF) textF.backgroundColor = .green textF.placeholdAttrAndAdjustFontFitWidth( placeholdStr: "123456789888", textFont: textF.font! , textColor: .blue, adjustsFontSizeToFitWidth: true) }