ios - 调整 UIPickerView 字体大小

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

如有任何帮助,我们将不胜感激。

最佳答案

您可以将 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
}

关于ios - 调整 UIPickerView 字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37996248/

相关文章:

iphone - 将 UIPickerView 添加到 UIActionSheet (底部按钮)

ios - TouchUpInside 事件未针对整个按钮框架触发

iOS 发送音频文件到网络服务器

ios - 如何使用 Storyboard停止 UINavigationBar 默认后退按钮动画?

ios - 在 app-prefix.pch 中导入所有类是好事还是坏事

ios - 如何在swift中初始化数组

ios - 喜欢来自原生 iOS 应用程序的 Facebook 页面

swift - 如何获得不同的插入和删除过渡动画?

ios - 在iphone或ipad的显示屏中央显示一个subview

swift - ActionPickerView 获取结果