David Sanford David Sanford - 5 months ago 29
Swift Question

Adjust UIPickerView font size

I see many solutions out there for the UIPickerView font size, but it seems they are all based on text based arrays and / or require a UIViewController.
I am using an Int Array within a UITableViewCell and cannot seem to find something that will work with what I am trying to accomplish.

Here is the code:

import UIKit


class AirTemperatureTableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource

{
let numberOfComponents: Int = 2
let temperatureComponentRows: Int = 501
let temperatureSymbolComponentRows: Int = 2

let Fahrenheit: String = "F"
let Celsius: String = "C"
let minDegrees = -250
let maxDegrees = 250

private var degrees: Array<Int> = [Int]()

var temperature: Int = 30 // our default temperature
var temperatureType: String = "C" // our default type is Celsius

@IBOutlet var airTemperaturePicker: UIPickerView!

override func awakeFromNib()
{
super.awakeFromNib()


for i in self.minDegrees ..< self.maxDegrees+1
{
self.degrees.append(i)
}

self.airTemperaturePicker.selectRow(280, inComponent: 0, animated: true)
}


override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return self.numberOfComponents
}


func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if component == 0
{
return self.temperatureComponentRows
}
else
{
return self.temperatureSymbolComponentRows
}
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
//
// If we are component 0, the degrees, return the value for the given row.
//
if component == 0
{
return String(self.degrees[row])
}
else
{
if row == 0
{
return self.Celsius
}
else
{
return self.Fahrenheit
}
}
}


func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
}
}


any help would be appreciated.

Answer

You can use a UILabel with the viewForRow method of the UIPickerViewDataSource. You can then configure it with all the regular label options incl. setting font size, color, etc.:

// goes in lieu of titleForRow if customization is desired
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {

    let pickerLabel = UILabel()

    if component == 0 {

        pickerLabel.textColor = .darkGrayColor()
        pickerLabel.textAlignment = .Center
        pickerLabel.text = String(self.degrees[row])
        pickerLabel.font = UIFont(name:"Helvetica", size: 28)
    } else {

        pickerLabel.textColor = .redColor()
        pickerLabel.textAlignment = .Center
        pickerLabel.font = UIFont(name:"Helvetica", size: 28)

        if row == 0 {
            pickerLabel.text = self.Celsius
        } else {
            pickerLabel.text = self.Fahrenheit
        }
    }
    return pickerLabel
}
Comments