23

How do I change the font size in a picker view? I have read a lot of questions about this, but none are in Swift 3. I have three picker views; the first two have two columns and have the same data source and delegate. The last one has one column and has a different data source and delegate. I can't fit the text in the first two picker views by one character. How do I shrink the font size in a UIPickerView and adjust the picker view row height, if necessary? Thanks.

class ViewController: UIViewController {

//MARK: Properties


@IBOutlet weak var layoutLengthPicker: UIPickerView!
@IBOutlet weak var layoutWidthPicker: UIPickerView!
@IBOutlet weak var trackPicker: UIPickerView!

let layoutLengthPickerDelegate = DimensionsPickerDelegate()
let layoutWidthPickerDelegate = DimensionsPickerDelegate()
let trackPickerDelegate = TrackPickerDelegate()

override func viewDidLoad() {
    super.viewDidLoad()

    layoutLengthPicker.delegate = layoutLengthPickerDelegate
    layoutLengthPicker.dataSource = layoutLengthPickerDelegate

    layoutWidthPicker.delegate = layoutWidthPickerDelegate
    layoutWidthPicker.dataSource = layoutWidthPickerDelegate

    trackPicker.delegate = trackPickerDelegate
    trackPicker.dataSource = trackPickerDelegate


    // Do any additional setup after loading the view, typically from a nib.
}


//MARK: Actions



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }


}

class DimensionsPickerDelegate: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {

let feet = ["0 Ft.", "1 Ft.", "2 Ft.", "3 Ft.", "4 Ft.", "5 Ft.", "6 Ft.", "7 Ft.", "8 Ft.", "9 Ft.", "10 Ft.", "11 Ft.", "12 Ft.", "13 Ft.", "14 Ft.", "15 Ft.", "16 Ft.", "17 Ft.", "18 Ft.", "19 Ft.", "20 Ft.", "21 Ft.", "22 Ft.", "23 Ft.", "24 Ft.", "25 Ft.", "26 Ft.", "27 Ft.", "28 Ft.", "29 Ft.", "30 Ft.", "31 Ft.", "32 Ft.", "33 Ft.", "34 Ft.", "35 Ft.", "36 Ft.", "37 Ft.", "38 Ft.", "39 Ft.", "40 Ft.", "41 Ft.", "42 Ft.", "43 Ft.", "44 Ft.", "45 Ft.", "46 Ft.", "47 Ft.", "48 Ft.", "49 Ft.", "50 Ft.", "51 Ft.", "52 Ft.", "53 Ft.", "54 Ft.", "55 Ft.", "56 Ft.", "57 Ft.", "58 Ft.", "59 Ft.", "60 Ft.", "61 Ft.", "62 Ft.", "63 Ft.", "64 Ft.", "65 Ft.", "66 Ft.", "67 Ft.", "68 Ft.", "69 Ft.", "70 Ft.", "71 Ft.", "72 Ft.", "73 Ft.", "74 Ft.", "75 Ft.", "76 Ft.", "77 Ft.", "78 Ft.", "79 Ft.", "80 Ft.", "81 Ft.", "82 Ft.", "83 Ft.", "84 Ft.", "85 Ft.", "86 Ft.", "87 Ft.", "88 Ft.", "89 Ft.", "90 Ft.", "91 Ft.", "92 Ft.", "93 Ft.", "94 Ft.", "95 Ft.", "96 Ft.", "97 Ft.", "98 Ft.", "99 Ft.", "100 Ft."]

let inches = ["0 In.", "1 In.", "2 In.", "3 In.", "4 In.", "5 In.", "6 In.", "7 In.", "8 In.", "9 In.", "10 In.", "11 In.", "12 In."]

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 2
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == 0 { return feet.count } else { return inches.count}

}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if component == 0 {return feet[row]} else {return inches[row]}

}
}

class TrackPickerDelegate: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {

let manufacturers = ["Atlas True Track", "Atlas Code 100", "Atlas Code 83", "Bachmann Nickel Silver", "Bachmann Steel Alloy", "Kato", "Life-Like Trains Code 100", "LIfe-Like Trains Power-Loc", "Peco Code 100", "Peco Code 83", "Peco Code 75", "Shinohara Code 100", "Shinohara Code 70", "Walthers"]

func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return manufacturers.count
    }

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return manufacturers[row]
    }

}
TonyStark4ever
  • 848
  • 1
  • 9
  • 24

2 Answers2

54

Try this for Swift 3.x:

Fill your Font name, Color, Size & Data Array with appropriate values.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    var pickerLabel: UILabel? = (view as? UILabel)
    if pickerLabel == nil {
        pickerLabel = UILabel()
        pickerLabel?.font = UIFont(name: "<Your Font Name>", size: <Font Size>)
        pickerLabel?.textAlignment = .center
    }
    pickerLabel?.text = <Data Array>[row]
    pickerLabel?.textColor = UIColor.blue

    return pickerLabel!
}

EDIT:

For Multiple components, you can do something like this:

if component == 0 {
     var label: UILabel? = (view as? UILabel)
     label.text = <Your Arr>[row]
     return label
}else {
     return anotherLabel
}

Output:

enter image description here

Hope it helps!!!

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
  • 3
    Note that you would use this `viewForRow` delegate method instead of the `titleForRow` delegate method. – rmaddy May 28 '17 at 05:08
  • @rmaddy Is that an issue? I did a demo, it works well. – Imad Ali May 28 '17 at 05:14
  • @Imad How do I use this with a two column picker view? I have a `feet` array and an `inches` array. In `viewForRow`, I currently have `if component == 0 {return feet[row]} else {return inches[row]}`. How do I implement this in `pickerLabel?.text = [row]`? Thanks. – TonyStark4ever May 28 '17 at 19:05
  • @Imad Also, curiosity question, can you change the font color in a picker view? – TonyStark4ever May 28 '17 at 19:21
  • 1
    @W.Cook For font color check my updated answer. And coming two column picker, you do something like this: if component == 0 { return label} else return anotherLabel. – Imad Ali May 29 '17 at 03:51
  • @Imad Here my code now: `func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { if component == 0 { var label: UILabel? = (view as? UILabel) label.text = feet[row] return label }else { var secondLabel: UILabel? = (view as? UILabel) secondLabel.text = inches[row] return secondLabel } }` However, I get this error: "Value of Optional type UILabel? not unwrapped; did you mean to use ? or !". What is wrong? Thanks. – TonyStark4ever May 29 '17 at 05:52
  • @Imad it also generates the error on the `label.text = feet[row]` line and the `label.text = inches[row]` line. Use an exclamation point here also? Thanks. – TonyStark4ever May 29 '17 at 06:10
  • @W.Cook Use label?.text = feet[row] – Imad Ali May 29 '17 at 06:12
  • @Imad Also, where do I put the `if pickerLabel == nil { pickerLabel = UILabel() pickerLabel?.font = UIFont(name: "", size: ) pickerLabel?.textAlignment = .center }`? After `func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { if component == 0 { var label: UILabel? = (view as? UILabel)`? Thanks. – TonyStark4ever May 29 '17 at 06:14
  • @kyo Fill data array with your data set. – Imad Ali Jan 20 '19 at 09:06
8

Update Swift 4.2

I managed to use the code below:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    var pickerLabel: UILabel? = (view as? UILabel)
    if pickerLabel == nil {
        pickerLabel = UILabel()
        pickerLabel?.font = UIFont(name: "Your Font Name", size: 25)
        pickerLabel?.textAlignment = .center
    }
    pickerLabel?.text = <Data Array>[component][row]
    pickerLabel?.textColor = UIColor(named: "Your Color Name")

    return pickerLabel!
}

Hopefully help you

Ali
  • 131
  • 1
  • 8