【问题标题】:Change the font color inside a Picker View更改选取器视图中的字体颜色
【发布时间】:2017-04-17 03:19:21
【问题描述】:

我正在创建货币转换器应用程序。应该使用选择器视图选择货币,所有数据都已经在里面。我的选择器视图字体当前是黑色的,但我想让它变成白色。如何更改字体的颜色(不是背景)?

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


    }

【问题讨论】:

  • picker.backgroundColor = UIColor.white 其中 picker 是您的 UIPIcker 对象的引用。

标签: ios swift xcode uipickerview


【解决方案1】:

要更改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】:

    你可以使用 attributesTitleForRow 和 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
        }
    

    【讨论】:

    • 谢谢你,它工作得很好!这是我在这里的第一个问题,我马上就得到了很好的答案,太棒了,谢谢你的帮助!
    • 欢迎。当有人用代码和可能的图像提出明确的问题时,让其他人更容易回答。你做得很好。好吧,您必须接受任何答案。
    • 但是如何更改 Picker View 中选定的特定索引处的文本颜色??
    • @SwiftyCodes 有 NSAttributedString,所以你可以改变字体颜色,字体等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    相关资源
    最近更新 更多