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

前端 未结 5 925
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 22:07

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(pickerVi         


        
5条回答
  •  别那么骄傲
    2020-12-30 22:25

    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.

提交回复
热议问题