2016-12-02 19 views
2

私は通貨変換アプリケーションを作成中です。通貨はピッカービューで選択する必要があります。すべてのデータはすでに内部にあります。私のピッカービューFontは現在黒ですが、私は白にしたいと思っています。フォントの色を変更するにはどうしたらいいですか?ピッカービュー内のフォントの色を変更します

func numberOfComponents(in pickerView: UIPickerView) -> Int 
{ 
    return 1 
} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
{ 
    return pickerViewSource.count 
} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
{ 
    return pickerViewSource[row] 
} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int 

    price_kind = pickerViewSource[row] 
    switch price_kind { 
    case "USD": 
     label.text = "$ " + price_kind 
     rate = 1.0 
    case "EUR": 
     label.text = "€ " + price_kind 
     rate = 0.9461 
    case "GBP": 
     label.text = "£ " + price_kind 
     rate = 0.8017 
    case "RUB": 
     label.text = "₽ " + price_kind 
     rate = 64.3435 
    case "CNY": 
     label.text = "¥ " + price_kind 
     rate = 6.9137 
    case "YEN": 
     label.text = "¥ " + price_kind 
     rate = 6.26 
    case "AUD": 
     label.text = "$ " + price_kind 
     rate = 1.3498 
    case "CAD": 
     label.text = "$ " + price_kind 
     rate = 1.3483 
    default: 
     label.text = "$ " + price_kind 
     rate = 1.0 


    } 
+0

** picker.backgroundColor = UIColor.white ** ** **ピッカーは、あなたのUIPIckerオブジェクトの参照です。 – Wolverine

答えて

2

UIPickerView使用NSAttributedString内のテキストを変更するには:

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

あなたはコードの下に参照してくださいattributedTitleForRowとNSAttributedString

を使用することができます。

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

あなたは別の色でそれぞれの行をしたい場合は、コードの下に使用します。

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 
     var attributedString: NSAttributedString! 

     switch component { 
     case 0: 
      attributedString = NSAttributedString(string: "Zero", attributes: [NSForegroundColorAttributeName : UIColor.redColor()]) 
     case 1: 
      attributedString = NSAttributedString(string: "One", attributes: [NSForegroundColorAttributeName : UIColor.redColor()]) 
     case 2: 
      attributedString = NSAttributedString(string: "Two"), attributes: [NSForegroundColorAttributeName : UIColor.redColor()]) 
     default: 
      attributedString = nil 
     } 

     return attributedString 
    } 
+1

ありがとう、それは完璧に働いた!これは私の最初の質問でした。すぐに素敵な回答がありました。本当にありがとうございました! – Jojo

+0

ようこそ。誰かが明確な質問をコードと考えて、他の人がよく答えるようにする可能性のあるイメージを置くとき。あなたはうまくいった。あなたは答えのどれかを受け入れなければなりません。 – Hasya

+0

しかし、ピッカービューで選択した特定のインデックスでテキストの色を変更するにはどうすればいいですか? –

関連する問題