代码之家  ›  专栏  ›  技术社区  ›  Alexa289

如何在autolayout中更改选择器视图的字体大小

  •  0
  • Alexa289  · 技术社区  · 8 年前

    enter image description here

    我正在尝试修复我的picker视图的自动布局,在iPhone上(紧凑宽度和规则高度)似乎可以,但对于iPad(规则宽度和规则高度),我的picker视图的字体大小似乎与iPhone相同,我希望我的picker视图在iPad中的字体大小稍微大一点,我通常为规则宽度和规则高度的字体大小添加自定义,但在界面生成器中没有调整选择器视图字体大小的选项。

    我该怎么办?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Rashwan L    8 年前

    这不能在界面生成器中完成,您需要以编程方式完成。有点简单,这样做:

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let pickerLabel = UILabel()
        if UIDevice.current.userInterfaceIdiom == .pad {
            pickerLabel.font = UIFont.systemFont(ofSize: 16)
            pickerLabel.text = "Row \(row)"
        } else if UIDevice.current.userInterfaceIdiom == .phone {
            pickerLabel.font = UIFont.systemFont(ofSize: 14)
            pickerLabel.text = "Row \(row)"
        }
        return pickerLabel
    }
    

    如果您使用 pickerView titleForRow 您需要删除该函数才能使用 viewForRow .