2015-03-24 13 views
5

Jak mogę zmienić kolor mojego selektora?Zmień kolor UIPicker

Nie chcę zmieniać czcionki ani tekstu. Mój selektor jest wypełniony i działa dokładnie tak, jak tego chcę, chcę zmienić kolor z czarnego na niestandardowy biały.

+0

zmienić kolor co: czcionka, tło, wybrana pozycja, niezaznaczone elementy? –

Odpowiedz

17

Spójrz: http://makeapppie.com/tag/uipickerview-in-swift/

Jeśli chcesz zmienić tytuł kolor każdego elementu można realizować:

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 
    let titleData = pickerData[row] 
    var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()]) 
    return myTitle 
} 

Swift 3:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 
    let titleData = pickerData[row] 
    let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.white]) 
    return myTitle 
} 
+0

To zrobiło dokładnie to, czego potrzebowałem i umożliwiło mi użycie białego tekstu w widoku próbnym na czarnym tle. Dziękuję –

+0

@Youri Nooijen, idealne dla mnie, dzięki. –

2

Jeśli chcesz mieć większą kontrolę dostosowywanie każdego elementu w selektorze ...

Użyj metody UIPickerViewDelegate's "viewForRow".

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 

    var label: UILabel 
    if let view = view as? UILabel { label = view } 
    else { label = UILabel() } 

    label.textColor = UIColor.blue 
    label.textAlignment = .center 
    label.font = UIFont(name: "My-Custom-Font", size: 20) // or UIFont.boldSystemFont(ofSize: 20) 
    label.adjustsFontSizeToFitWidth = true 
    label.minimumScaleFactor = 0.5 
    label.text = getTextForPicker(atRow: row) // implemented elsewhere 

    return label 
} 

Oczywiście widok, który powracasz, może być bardziej skomplikowany niż tylko UILabel.

0

Swift 4,0

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 
    let titleData = arrayOfPickerData[row] 
    let myTitle = NSAttributedString(string: titleData, attributes: [NSAttributedStringKey.font:UIFont(name: "Georgia", size: 15.0)!,NSAttributedStringKey.foregroundColor:UIColor.white]) 
    return myTitle 
}