如何在Swift中使用多个组件更改UIPickerView的文本颜色?
下面的代码将更改所有3个组件的选择器视图的字体颜色。但是,当我尝试旋转车轮时会崩溃。我认为这与didSelectRow函数有关。也许这两个功能必须以某种方式嵌套?任何想法?
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
}
回答:
以下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
}
如果要attributedString
在多个if
或switch
语句中使用,下面的UIViewController
subClass示例将为您提供帮助:
import UIKitclass 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中使用多个组件更改UIPickerView的文本颜色? 的全部内容, 来源链接: utcz.com/qa/411224.html