[英]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. 我看到了许多针对UIPickerView字体大小的解决方案,但似乎它们都基于基于文本的数组和/或需要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.
我在UITableViewCell中使用一个Int数组,似乎无法找到与我要完成的工作一起使用的东西。
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: 您应该在
UITableViewCell
子类中使用UIPickerViewDelegate
函数viewForRow
如下所示:
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. picker
是UITableView
子类集中的UIPickerView
的@IBOutlet
,它使用原型单元格或外部笔尖和控件拖动。 Also, make sure to set the picker
's delegate
and dataSource
properties inside awakeFromNib
. 另外,请确保在
awakeFromNib
设置picker
的delegate
和dataSource
属性。
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. 这应该使单元格宽度pickerView带有您选择的字体的UILabel。
You can use a UILabel with the viewForRow method of the UIPickerViewDataSource. 您可以将UILabel与UIPickerViewDataSource的viewForRow方法一起使用。 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.