Search code examples
iosswiftuipickerview

changing font size and style in picker view


How can I change font style and size of picker view? I don't see an option in the picker view settings. Can you set it programmatically? I'm using swift 3 and xcode 8.2.


Solution

  • You can change font style and size in function:

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

    For example:

    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
      }
    }