如何在Swift中更改UIPickerView多个组件的文本颜色?

20

以下代码将更改所有三个组件的选择器视图的字体颜色。但是,当我尝试旋转轮子时,它会崩溃。我认为这与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
    }

4
你尝试过什么吗?在询问之前,你应该努力尝试一些方法。 - lagivan
1
当然,我花了数小时的时间,参加了两门课程——从零开始,没有编程背景,连续一周不停地开发这个应用程序。但是很难,所有的教程都在playground中。所以,虽然我能理解逻辑,但我很难理解代码放在哪里。所有的初始化(我想它被称为)和Swift/Object C的范围。 - KML
作用域。如果你不理解像“var mySpeechSynthesizer: AVSpeechsynthesizer…”这样的语句是什么意思,它应该放在哪里,以及如何调用它们以及调用它们的顺序和名称,那么知道在Playground中如何编写switch/case语句也没有帮助。我甚至不知道我不知道的东西的名字...哈哈,你怎么能在不问别人的情况下弄清楚这些东西呢?告诉我吧,因为我在电子书、课程或Youtube上都找不到答案。 - KML
5个回答

35
以下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
}

更新

如果你想在多个ifswitch语句中使用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
        }
    }

}

嗨,非常感谢,这改变了颜色!但是它也改变了数组 - 选择器视图中的文本。我猜我应该用数组的名称替换它?还有另一个复杂因素,我有3个组件(即3列)。 - KML
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{ switch component { case 0: return a[row] case 1: return b[row] case 2: return c[row] default: return "oops" } } - KML
谢谢您的回复!代码行:let attributedString: NSAttributedString! --> 错误/黄色:不可变值被默认初始化,永远无法更改。代码行:attributedString = NSAttributedstring.... --> 错误/红色:无法分配给“let”值“attributedString”。 - KML
哇,那差不多成功了!现在选择器视图已经改变颜色,并且组件已经“初始化为数组中的第一个值。但是,当我滑动轮子时,它仍停留在数组的第一个值。Xcode还在 func pickerView(pickerView:UIPickerView,didSelectRow row:Int,inComponent component:Int)的第一次迭代上显示了一个绿色断点。我认为你的代码必须以某种方式“嵌套”在其他代码中或者反之,以便轮子填充a.text、b.text和c.text中的所有值。我会更新问题。 - KML
我已经编辑了我的答案,其中包含一个UIViewController子类,其中包含一个带有3个组件和红色文本的UIPickerView(IBOutlet)。在Xcode单视图应用程序模板中使用它工作正常。 - Imanou Petit
显示剩余4条评论

18

Swift 4.0

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    return NSAttributedString(string: pickerData[row], attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
}

8

Swift 3

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

2

Swift 5

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    return NSAttributedString(string: pickerData[row], attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
}

1

在此输入图像描述这段代码片段对我有效

extension UIPickerView {
@IBInspectable var pickerTextColor:UIColor? {
    get {
        return self.pickerTextColor
    }
    set {
        self.setValue(newValue, forKeyPath: "textColor")
    }
}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接