简体   繁体   中英

Change the font color inside a Picker View

I am in process of creating a currency converter app. The currency should be selected with a picker view, all the data is already inside. My picker view Font is currently black, but I want to have it white. How would I change the color of the font (not the background)?

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


    }

To change the text inside the UIPickerView use NSAttributedString :

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

You can use attributedTitleForRow and NSAttributedString

Please refer below code.

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

If you want each row with different color, then use below code.

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM