【问题标题】:How to change Searchbar's Placeholder font and size in iOS13如何在 iOS13 中更改 Searchbar 的占位符字体和大小
【发布时间】:2020-02-12 01:28:11
【问题描述】:

我阅读了几个堆栈溢出条目来更改搜索栏的占位符文本属性。但是,在 iOS13 中,它们都没有真正起作用。

不知iOS13下searchBar占位符的字体、字体大小和字体颜色如何改变?

这是我尝试过的:

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 placeholder uisearchcontroller ios13 searchbar


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    斯威夫特 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
    
                }
    

    【讨论】:

    • 适用于 iOS13.1。有点令人困惑的是,Apple 踢出了许多.value(forKey: ...)fields - 但显然不是这种情况......!再次感谢!
    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2019-04-01
    • 2014-03-15
    • 1970-01-01
    • 2016-07-17
    • 2017-01-14
    相关资源
    最近更新 更多