如何在 iOS13 中更改搜索栏的占位符字体和大小

How to change Searchbar's Placeholder font and size in iOS13

我阅读了几个堆栈溢出条目来更改 searchBar 的占位符文本属性。但是,在 iOS13 中,none 确实有效。

我想知道如何在 iOS13 下更改 searchBar Placeholder 的字体、字体大小和字体颜色?

这是我尝试过的:

let myAttributes = [NSAttributedString.Key.font: UIFont(name: "Avenir-Heavy", size: 28) as Any]

navigationItem.searchController?.searchBar.placeholder = 
NSAttributedString(string: "placeholder text", attributes: myAttributes).string

Swift 5:

 if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
                let atrString = NSAttributedString(string: "Search",
                                                   attributes: [.foregroundColor : color,
                                                                .font : UIFont.systemFont(ofSize: 10, weight: .bold)])
                textfield.attributedPlaceholder = atrString

            }

您可以在不访问键值的情况下使用此方法,因为 searchbarsearchTextField 属性。在初始化搜索栏后添加此代码。

let placeholder = NSAttributedString(string: "your placeholder text", attributes: [.foregroundColor: UIColor.gray, NSAttributedString.Key.font: UIFont(name: "Helvetica", size: 15)!])
searchBar.searchTextField.attributedPlaceholder = placeholder