【问题标题】:How do I change the text color of UIPickerView with multiple components in Swift?如何在 Swift 中使用多个组件更改 UIPickerView 的文本颜色?
【发布时间】:2014-11-12 02:00:31
【问题描述】:

以下代码将更改所有 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
    }

【问题讨论】:

  • 你有没有尝试过?在提问之前,您应该尝试并做出一些努力。
  • 是的,当然,花费数小时和数小时,参加了 2 门课程 - 从零开始,在没有编程背景的情况下不间断地在这个应用程序上工作了一周。但这很难,所有的教程都在操场上。因此,虽然我的逻辑是正确的,但我无法理解代码在哪里。 swift/object C的所有初始化(我认为它被调用)和范围。
  • 和范围。如果您不了解诸如“var mySpeechSynthesizer: AVSpeechsynthesizer...”之类的语句是什么,它的去向以及调用它们的顺序以及什么,那么知道如何在 Playground 中编写 switch/case 语句也无济于事打电话给他们。我什至不知道我不知道的东西的名字......大声笑你怎么不问就知道这样的东西,告诉我,因为我在电子书、课程或 Youtube 上找不到它

标签: ios xcode swift uipickerview xcode6gm


【解决方案1】:

下面的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 列。
  • func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{ return 3 } func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ switch component { case 0: return a.count case 1 : return b.count case 2: return c.count default: return 10 } }
  • 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:返回 c[row] 默认值:返回 "oops" } }
  • 感谢您的回复!行:让属性字符串:NSAttributedString! --> 错误/黄色:不可变值是默认初始化的,永远无法更改。 Line(s) attributesString = NSAttributedstring.... --> 错误/红色:无法分配给 'let' 值 'attributedString'
  • 哇,几乎成功了!现在选择器视图已经改变了颜色,并且组件“使用数组中的第一个值初始化。但是,当我轻弹滚轮时,它会停留在数组中的第一个值。Xcode 在第一次迭代时也会显示一个绿色断点func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){。我认为您的代码必须以某种方式“嵌套”在其他代码中,反之亦然,以便轮子充满 a.text 中的所有值, b.text 和 c.text。我会更新问题。
【解决方案2】:

Swift 4.0

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

【讨论】:

    【解决方案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
        }
    

    【讨论】:

      【解决方案4】:

      enter image description here此代码 sn-p 对我有用

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

      【讨论】:

        【解决方案5】:

        斯威夫特 5

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-14
          • 1970-01-01
          • 2014-12-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多