ios - 更改选取器 View 内的字体颜色

标签 ios swift xcode uipickerview

我正在创建一个货币转换器应用程序。应使用选择器 View 选择货币,所有数据都已在其中。我的选择器 View 字体当前为黑色,但我想将其设置为白色。我如何更改字体的颜色(而不是背景)?

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


    }

最佳答案

要更改 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])
}

关于ios - 更改选取器 View 内的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40928383/

相关文章:

ios - 如何正确换行长文本?

arrays - Swift:遍历字典数组

ios - 无法在 Xcode 中打开数据模型文件

ios - 视频/电影播放时间

objective-c - 使用委托(delegate)将数组从一个类传递到另一个类 - 最佳设计模式?

xcode - Swift 事件指示器和标签

iphone - 核心图 : Updating X Axis range in real time plotting

objective-c - 在 Xcode 6 中关闭指定初始化程序检查

ios - 符合 UIApperance 的自定义类允许使用哪些 Swift 属性类型?

ios - ScrollView 和导航栏 SwiftUI 的屏幕背景颜色