【问题标题】:How to increase a scope title and search bar's font size in Xcode?如何在 Xcode 中增加范围标题和搜索栏的字体大小?
【发布时间】:2019-02-09 00:02:58
【问题描述】:

我的应用中有一个包含 3 个范围标题的搜索栏标签。 在 iPhone 上,这些标题的字体大小很好,但在 iPad 上太小了,我找不到 R.R 功能可以像故事板中的普通文本一样更改! 有什么简单的方法可以解决这个问题吗?

【问题讨论】:

    标签: ios swift xcode uisearchbar


    【解决方案1】:

    setScopeBarButtonTitleTextAttributes:forState: 为给定状态的搜索栏按钮的标题字符串设置文本属性。

     let font = UIFont.systemFont(ofSize: 24)   
     self.searchBar.setScopeBarButtonTitleTextAttributes([NSAttributedString.Key.font : font], for: .normal)
    

    在 cmets 中回答您的问题。

    更新搜索栏中UITextField的字体大小

    通过迭代子视图从searchBar中提取UITextField对象,然后更新它的字体。

    extension UISearchBar {
        func update(font : UIFont?) {
            for view in (self.subviews[0]).subviews {
                if let textField = view as? UITextField {
                    textField.font = font
                }
            }
        }
    }
    

    在初始化 searchBar 对象时使用此方法更新字体

    @IBOutlet weak var searchBar:UISearchBar!{
            didSet {
                searchBar.update(font: UIFont.systemFont(ofSize: 30))
            }
        }
    

    【讨论】:

    • 它没有为我改变尺寸?我应该在 Viewdidload 中使用吗?
    • 是的,在viewDidLoad 方法中使用这行代码并确保searchBar 是从@IBOutlet 连接或以编程方式启动的。
    • 很好用,还有一件事,“搜索栏占位符文本”也很小!有什么解决办法吗?
    • @itch 检查搜索栏占位符文本的更新答案。
    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2012-09-12
    相关资源
    最近更新 更多