向 UISearchBar 添加动态类型

Har*_*ish 6 accessibility uisearchbar ios swift dynamic-type-feature

我的问题在标题中已经说了很多。我正在尝试将动态类型(定义为here)添加到 aUISearchBar没有运气。我知道这是可能的,因为系统应用程序似乎能够很好地处理它,如下所示: 在此处输入图片说明

但是,我的应用程序似乎处理得并不好,如下所示:

在此处输入图片说明

知道里面UITextField包含UISearchBar我很自然地尝试了这个解决方案,但没有成功: UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).adjustsFontForContentSizeCategory = true

我也尝试过在线搜索/检查文档,但似乎在任何地方都找不到解决方案。我是否缺少让动态类型在UISearchBar.

更新:@matt 建议我手动检查并以这种方式更新字体。但是,这会产生另一个问题,因为搜索栏本身太小而无法容纳文本,如下所示: 在此处输入图片说明

@matt 建议也使用该scaledValue(for:)方法更新高度,但这似乎不起作用。这是我正在使用的代码:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.preferredFont(forTextStyle: .body)
        let textFieldFrame = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).frame
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).frame = CGRect(x: textFieldFrame.minX, y: textFieldFrame.minY, width: textFieldFrame.width, height: UIFontMetrics.default.scaledValue(for: textFieldFrame.height))
}
Run Code Online (Sandbox Code Playgroud)

字体现在似乎在用这个更新的代码缩放,但搜索栏的高度没有增长:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    searchBar.textField?.font = UIFont.preferredFont(forTextStyle: .body)
    if let textFieldFrame = searchBar.textField?.frame {
        searchBar.textField?.frame = CGRect(x: textFieldFrame.minX, y: textFieldFrame.minY, width: textFieldFrame.width, height: UIFontMetrics.default.scaledValue(for: textFieldFrame.height))
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,这是我找到 textField 的方法(以防其他卡住的用户想知道):

extension UISearchBar {
    var textField: UITextField? {
        var _textField: UITextField? = nil
        subviews.forEach {
            $0.subviews.forEach {
                if let textField = $0 as? UITextField {
                    _textField = textField
                }
            }
        }
        return _textField
    }
}
Run Code Online (Sandbox Code Playgroud)

XLE*_*_22 2

我遵循以下里程碑来实现您的目标:

  • 使用动态类型功能自动调整字体(第 1 步)
  • 当出现新的首选内容大小类别时,调整搜索栏约束(步骤 2) 及其文本字段约束(步骤 3)

    class SearchBarDynamicTypeVC: UIViewController {
    
    @IBOutlet weak var mySearchBar: UISearchBar!
    
    let fontHead = UIFont(name: "Chalkduster", size: 20.0)
    let fontHeadMetrics = UIFontMetrics(forTextStyle: .title1)
    var initialFrameHeight: CGFloat = 0.0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = fontHeadMetrics.scaledFont(for: fontHead!)
    
        mySearchBar.textField?.adjustsFontForContentSizeCategory = true //STEP 1
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        initialFrameHeight = mySearchBar.intrinsicContentSize.height
    
        if let textField = mySearchBar.textField {
            adaptConstraints(textField) //Initialization according to the first preferred content size category
        }
    }
    
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
    
        if let textField = mySearchBar.textField,
            let _ = previousTraitCollection {
    
            adaptConstraints(textField) // STEP 2 & 3
        }
    }
    
    
    private func adaptConstraints(_ textField: UITextField) {
    
    //  Adapt the SEARCHBAR constraints
        mySearchBar.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.deactivate(mySearchBar.constraints)
    
        let heightSB = mySearchBar.heightAnchor.constraint(equalToConstant: fontHeadMetrics.scaledValue(for: initialFrameHeight))
        let widthSB = mySearchBar.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 20)
        let centerVSB = mySearchBar.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
        let centerHSB = mySearchBar.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
    
        NSLayoutConstraint.activate([centerVSB,
                                     centerHSB,
                                     widthSB,
                                     heightSB])
    
    //  Adapt the SEARCHBAR TEXTFIELD constraints
        textField.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.deactivate(textField.constraints)
    
        let centerXTF = textField.centerXAnchor.constraint(equalTo: textField.superview!.centerXAnchor)
        let centerYTF = textField.centerYAnchor.constraint(equalTo: textField.superview!.centerYAnchor)
        let widthTF = textField.widthAnchor.constraint(equalTo: textField.superview!.widthAnchor, constant: -20.0)
        let heightTF = textField.heightAnchor.constraint(equalTo: textField.superview!.heightAnchor, constant: -20.0)
    
        NSLayoutConstraint.activate([centerXTF,
                                     centerYTF,
                                     widthTF,
                                     heightTF])
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)

我使用您的帖子中提供的代码片段来获取搜索栏文本字段:

extension UISearchBar {

    var textField: UITextField? {

        var _textField: UITextField? = nil

        subviews.forEach {
            $0.subviews.forEach {
                if let textField = $0 as? UITextField {
                    _textField = textField
                }
            }
        }
        return _textField
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,您也可以使用密钥获取它,searchField如下所示:

let textField = searchBar.value(forKey: "searchField") as? UITextField
Run Code Online (Sandbox Code Playgroud)

下面的快照显示了最终结果:

在此输入图像描述

现在,您可以通过调整上面的代码片段并自定义视觉个人选择(文本样式、字体、边距...)来向 UISearchBar 添加动态类型