简体   繁体   English

如何阻止 UITextField 的属性占位符文本在 iOS 13 上被截断?

[英]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.虽然它在 iOS 11-12.* 上正常工作,但在 iOS 13 上,它的框架变小,因此占位符被截断。 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)])
        }
    }

在此处输入图像描述

One solution would be to add an empty view inside your textField as its leftView property.一种解决方案是在 textField 中添加一个空视图作为其 leftView 属性。

Wherever you set up your textField, say in your view controller in viewDidLoad, you can add the following:无论您在何处设置 textField,例如在 viewDidLoad 中的视图 controller 中,您都可以添加以下内容:

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

After a lot of headache I figured out the solution.经过很多头痛后,我想出了解决方案。

Solution : I was adding leftView and rightView to maintain the padding.解决方案:我正在添加 leftView 和 rightView 来维护填充。 Instead of adding a view to the rightView of the textfield you need to add a button.您需要添加一个按钮,而不是向文本字段的 rightView 添加视图。 Don't know why it works but it works.不知道为什么它有效,但它有效。 If someone has more to add you are welcome.如果有人有更多要添加的,欢迎您。

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

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