栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Swift中使用多个组件更改UIPickerView的文本颜色?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Swift中使用多个组件更改UIPickerView的文本颜色?

以下

pickerView:attributedTitleForRow:forComponent:
方法实现应为您提供帮助:

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

更新资料

如果要

attributedString
在多个
if
switch
语句中使用,下面的
UIViewController
subClass示例将为您提供帮助:

import UIKitclass 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: Int) -> 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, attributedTitleForRow row: Int, forComponent component: Int) -> 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, didSelectRow row: Int, inComponent component: Int) {        switch component {        case 0: println(arrayOne[row])        case 1: println(arrayTwo[row])        case 2: println(arrayThree[row])        default: break        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/613986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号