UIPickerView won't allow changing font name and size via delegate's `attributedTitleForRow…`

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

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.

回答1:

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 } 

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



回答2:

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  } 


回答3:

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  } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!