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

前端 未结 5 901
爱一瞬间的悲伤
爱一瞬间的悲伤 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:29

    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
    }
    

提交回复
热议问题