简体   繁体   中英

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.

You should use the UIPickerViewDelegate function viewForRow inside your UITableViewCell subclass like so:

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
   let view = UIView(frame: picker.frame)
   let label = UILabel(frame: picker.frame)
   label.textAlignment = .Center
   label.text = String(degrees[row])
   label.font = UIFont(name: "Arial", size: 15)
   label.adjustsFontSizeToFitWidth = true
   view.addSubview(label)

   return view
}

picker is an @IBOutlet for your UIPickerView in your UITableView subclass set using either the prototype cell or external nib and control-drag. Also, make sure to set the picker 's delegate and dataSource properties inside awakeFromNib .

override func awakeFromNib() {
    super.awakeFromNib()
    picker.dataSource = self
    picker.delegate = self
    //Your Code Below
}

This should make a cell width pickerView with a UILabel of your font choice.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM