30

I use the following func to change the font color and font size, the color works but the font name and font size refuse to work.

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? 

var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Arial-BoldMT", size: 45)!, NSForegroundColorAttributeName:UIColor.whiteColor()])

any help?

Thx.

Hari Honor
  • 8,677
  • 8
  • 51
  • 54
jdleung
  • 1,088
  • 2
  • 10
  • 26

5 Answers5

59

I have a another option may be it helpfull for you..

Do all work that need for normal picker view : for more help UIPickerView make in Swift iOS

Now follow this step : - you can use method of viewForRow like this

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    var pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()
    pickerLabel.text = "PickerView Cell Title"
   // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
}

Updated Swift 3

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.black
    pickerLabel.text = "PickerView Cell Title"
    // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.center
    return pickerLabel
}

Updated Swift 5

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView
    {
        let pickerLabel = UILabel()
        pickerLabel.textColor = UIColor.white
        pickerLabel.text = "PickerView Cell Title"
        // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
        pickerLabel.font = UIFont.boldSystemFont(ofSize: 20) // In this use your custom font
        pickerLabel.textAlignment = NSTextAlignment.center
        return pickerLabel
    }

May be it help full for you, I also used this in my project.

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
  • 1
    Yes, your codes work. But I need to get the recalculated title data from pickerView(pickerView, titleForRow: row, forComponent: component). By the way, how can I post codes in comment? – jdleung Dec 13 '14 at 12:00
  • `pickerView(_:titleForRow:forComponent:)`
    `pickerView(_:attributedTitleForRow:forComponent:)`
    `pickerView(_:viewForRow:forComponent:reusingView:)`
    the three above can also show the pickerview content, but should not use them at the same time, I somehow use two of them. Now only using the last one and problem solved.
    Thanks all.
    – jdleung Dec 13 '14 at 14:17
9

You can declare the datasource for pickerview

let arrDataSource:[String] = ["row 1","row 2","row 3"]

then use this array of string as title for row in below function

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView

{
    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()
    pickerLabel.text = arrDataSource[row]
     pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    //pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
 }
L.N.Vu
  • 369
  • 4
  • 8
2

SWIFT 3

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

    var string = ""

    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.white
    pickerLabel.text = string
    pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22) // In this use your custom font
    pickerLabel.textAlignment = .center

    return pickerLabel

}
BennyTheNerd
  • 3,930
  • 1
  • 21
  • 16
2

Swift 4.1 / Xcode 9.4.1

Obviously you can select you own font, but here I'm using System Bold 13.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let pickerLabel = UILabel()
    pickerLabel.font = UIFont.boldSystemFont(ofSize: 13)
    pickerLabel.textColor = UIColor.black
    pickerLabel.textAlignment = .center
    pickerLabel.text = "PickerView Cell Title"

    return pickerLabel
}
drewster
  • 5,460
  • 5
  • 40
  • 50
1

To further elaborate on https://stackoverflow.com/users/4020910/bennythenerd answer with UILabel I'd go with a bit more memory optimised solution that reuses the labels:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let pickerLabel: UILabel
    if let label = view as? UILabel {
        pickerLabel = label
    } else {
        pickerLabel = UILabel()
        pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22)
        pickerLabel.textAlignment = .center
    }
    pickerLabel.text = pickerData[row] //This is your string
    return pickerLabel
}
Tanel Teemusk
  • 707
  • 8
  • 17