调整UIPickerView字体大小

时间:2016-06-23 15:45:23

标签: ios swift uipickerview

我看到UIPickerView字体大小有很多解决方案,但似乎它们都基于基于文本的数组和/或需要UIViewController。 我在UITableViewCell中使用Int数组,似乎无法找到适合我想要实现的东西。

以下是代码:

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您应该使用UIPickerViewDelegate子类中的viewForRow函数UITableViewCell,如下所示:

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是您@IBOutlet子集中UIPickerView的{​​{1}},使用原型单元格或外部笔尖和控制拖动。另外,请务必在UITableView内设置picker的{​​{1}}和delegate属性。

dataSource

这应该使用您的字体选择的UILabel制作单元格宽度pickerView。

答案 1 :(得分:1)

您可以将UILabel与UIPickerViewDataSource的viewForRow方法一起使用。然后,您可以使用所有常规标签选项配置它。设置字体大小,颜色等:

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