【问题标题】:How to stop a UITextField's attributed placeholder text from getting cut off on iOS 13?如何阻止 UITextField 的属性占位符文本在 iOS 13 上被截断?
【发布时间】:2020-07-21 10:47:57
【问题描述】:

我正在为搜索栏创建一个自定义文本字段。我需要自定义字体和字体颜色。虽然它在 iOS 11-12.* 上正常工作,但在 iOS 13 上它的框架变小,因此占位符被截断。仅供参考,我为此使用了属性占位符。我怎样才能阻止这种情况?

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)])
        }
    }

【问题讨论】:

    标签: ios swift uikit uitextfield ios13


    【解决方案1】:

    一种解决方案是在 textField 中添加一个空视图作为其 leftView 属性。

    无论您在哪里设置 textField,例如在 viewDidLoad 中的视图控制器中,您都可以添加以下内容:

            //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
    

    【讨论】:

    • 使用左右视图的填充已经存在。
    • 只是为了确保您的文本字段的 leftViewMode 属性设置为 .always?
    【解决方案2】:

    经过一番头疼后,我想出了解决方案。

    解决方案:我添加了 leftView 和 rightView 来保持填充。您需要添加一个按钮,而不是向文本字段的 rightView 添加视图。不知道为什么它有效,但它有效。如果有人有更多要补充的欢迎您。

    【讨论】:

      猜你喜欢
      • 2016-05-10
      • 2015-12-21
      • 1970-01-01
      • 2017-01-01
      • 2014-09-02
      • 2019-06-22
      • 2016-03-14
      • 2020-09-03
      • 1970-01-01
      相关资源
      最近更新 更多