在选择器视图中更改字体大小和样式

时间:2017-07-19 02:47:31

标签: ios swift uipickerview

如何更改选择器视图的字体样式和大小?我没有在选择器视图设置中看到一个选项。你能以编程方式设置它吗?我正在使用swift 3和 xcode 8.2。

1 个答案:

答案 0 :(得分:6)

您可以在功能中更改字体样式和大小:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView

例如:

let pickerDataSource: [String] = (1...10).map { "Option " + String(format: "%2d", $0)}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let label = (view as? UILabel) ?? UILabel()

    label.textColor = .red
    label.textAlignment = .center
    label.font = UIFont.systemFont(ofSize: 20)

    label.text = pickerDataSource[row]

    return label
  }
}