[英]How do I change the text color of UIPickerView with multiple components in Swift?
Below code will change the font colour of the picker view of all 3 components.下面的代码将更改所有 3 个组件的选择器视图的字体颜色。 However, it crash when I try to spin the wheel.但是,当我尝试旋转轮子时它会崩溃。 I think it has to do with the didSelectRow function.我认为这与 didSelectRow 函数有关。 Maybe the two function have to be nested somehow?也许这两个函数必须以某种方式嵌套? Any idea?任何的想法?
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
var attributedString: NSAttributedString!
if component == 0 {
attributedString = NSAttributedString(string: a.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
if component == 1 {
attributedString = NSAttributedString(string: b.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
if component == 2 {
attributedString = NSAttributedString(string: c.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
return attributedString
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
switch component {
case 0:
aOutput.text = a[row] --> **Code breaks**
case 1:
bOutput.text = b[row]
case 2:
cOutput.text = c[row]
default:
10
}
The following pickerView:attributedTitleForRow:forComponent:
method implementation should help you:以下pickerView:attributedTitleForRow:forComponent:
方法实现应该可以帮助您:
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let attributedString = NSAttributedString(string: "some string", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
return attributedString
}
Update更新
If you want to use attributedString
in multiple if
or switch
statements, the following UIViewController
subClass example will help you:如果你想在多个if
或switch
语句中使用attributedString
,下面的UIViewController
示例将帮助你:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
let arrayOne = ["One", "Two", "Three", "Four", "Five", "Six"]
let arrayTwo = ["Un", "Deux", "Trois", "Quatre", "Cinq", "Six"]
let arrayThree = [1, 2, 3, 4, 5, 6]
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
}
func numberOfComponentsInPickerView(_: UIPickerView) -> Int {
return 3
}
func pickerView(_: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return arrayOne.count
case 1:
return arrayTwo.count
case 2:
return arrayThree.count
default:
return NSNotFound
}
}
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
var attributedString: NSAttributedString!
switch component {
case 0:
attributedString = NSAttributedString(string: arrayOne[row], attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
case 1:
attributedString = NSAttributedString(string: arrayTwo[row], attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
case 2:
attributedString = NSAttributedString(string: toString(arrayThree[row]), attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
default:
attributedString = nil
}
return attributedString
}
func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch component {
case 0:
println(arrayOne[row])
case 1:
println(arrayTwo[row])
case 2:
println(arrayThree[row])
default:
break
}
}
}
Swift 4.0斯威夫特 4.0
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: pickerData[row], attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
}
Swift 3斯威夫特 3
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let attributedString = NSAttributedString(string: "YOUR STRING", attributes: [NSForegroundColorAttributeName : UIColor.white])
return attributedString
}
enter image description here This code snippet works for me在此处输入图像描述此代码段对我有用
extension UIPickerView {
@IBInspectable var pickerTextColor:UIColor? {
get {
return self.pickerTextColor
}
set {
self.setValue(newValue, forKeyPath: "textColor")
}
}}
Swift 5斯威夫特 5
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: pickerData[row], attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.