如何在Swift中使用多个组件更改UIPickerView的文本颜色? [英] How do I change the text color of UIPickerView with multiple components in Swift?

查看:145
本文介绍了如何在Swift中使用多个组件更改UIPickerView的文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码将更改所有3个组件的选择器视图的字体颜色。但是,当我试图旋转车轮时,它会崩溃。我认为它与didSelectRow函数有关。也许两个函数必须以某种方式嵌套?有什么想法吗?

Below code will change the font colour of the picker view of all 3 components. However, it crash when I try to spin the wheel. I think it has to do with the didSelectRow function. 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
    }


推荐答案

关注 pickerView:attributionTitleForRow:forComponent:方法实现应该可以帮助你:

The following pickerView:attributedTitleForRow:forComponent: method implementation should help you:

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let attributedString = NSAttributedString(string: "some string", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
    return attributedString
}

更新

如果你想使用 attributionString 在多个如果切换语句中,以下 UIViewController subClass示例将帮助您:

If you want to use attributedString in multiple if or switch statements, the following UIViewController subClass example will help you:

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中使用多个组件更改UIPickerView的文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆