gpt4 book ai didi

ios - 调整 UIPickerView 字体大小

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:35 24 4
gpt4 key购买 nike

我看到许多针对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com