iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何在Swift中使用多个组件更改UIPickerView的文本颜色?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

下面的代码将更改所有3个组件的选择器视图的字体颜色.但是,当我试图旋转车轮时,它会崩溃.我认为它与didSELEctRow函数有关.也许两个函数必须以某种方式嵌套?任何想法? func pickerView(pickerView: UIPickerView, attributedtitleForRow row: Int, forComponent component: @R_@R_696_11241@_10185@ -> NSAttr
下面的代码将更改所有3个组件的选择器视图的字体颜色.但是,当我试图旋转车轮时,它会崩溃.我认为它与didSELEctRow函数有关.也许两个函数必须以某种方式嵌套?任何想法?

func pickerView(pickerView: UIPickerView,attributedtitleForRow row: Int,forComponent component: @R_@R_696_11241@_10185@ -> 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: @R_@R_696_11241@_10185@{

    switch component {
    case 0:
        aOutput.text = a[row]      -->  **Code breaks**
    case 1:
        bOutput.text = b[row]
    case 2:
        cOutput.text = c[row]
    default:
        10
    }

解决方法

以下pickerView:attributiontitleForRow:forComponent:方法实现应该可以帮助您:

func pickerView(pickerView: UIPickerView,forComponent component: @R_@R_696_11241@_10185@ -> NSAttributedString? {
    let attributedString = NSAttributedString(String: "some String",attributes: [NSForegroundColorAttributename : UIColor.redColor()])
    return attributedString
}

更新

如果要在多个if或switch语句中使用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: @R_@R_696_11241@_10185@ -> 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,forComponent component: @R_@R_696_11241@_10185@ -> 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,inComponent component: @R_@R_696_11241@_10185@ {
        switch component {
        case 0:
            println(arrayOne[row])
        case 1:
            println(arrayTwo[row])
        case 2:
            println(arrayThree[row])
        default:
            break
        }
    }

}

大佬总结

以上是大佬教程为你收集整理的ios – 如何在Swift中使用多个组件更改UIPickerView的文本颜色?全部内容,希望文章能够帮你解决ios – 如何在Swift中使用多个组件更改UIPickerView的文本颜色?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。