Search code examples
swiftuipickerviewnsattributedstring

Setting font using NSAttributedString


Trying change font in my pickerView using NSAttributedString:

public func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    guard let castDict = self.castDict else {
        return nil
    }
    let name = [String](castDict.keys)[row]
    switch component {
    case 0:
        return NSAttributedString(string: name, attributes: [NSForegroundColorAttributeName : AppColors.Rose.color, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)])
    case 1:
        guard let character = castDict[name] else {
            return NSAttributedString(string: "Not found character for \(name)", attributes: [NSForegroundColorAttributeName : AppColors.Rose.color, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)])
        }
        return NSAttributedString(string: character, attributes: [NSForegroundColorAttributeName : AppColors.LightBlue.color, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 14)])
    default:
        return nil
    }
}

Color changed, font - not:

enter image description here

What I am doing wrong?


Solution

  • The short answer would be that you are doing nothing wrong, it's a problem on Apple's part, since they didn't write anywhere that Fonts cannot be changed in an UIPickerView.

    However, there is a workaround.

    From the UIPickerViewDelegate you have to implement func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView. With this implemented, you will be able to provide a custom UIView for each row.

    Here's an example:

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        if let pickerLabel = view as? UILabel {
            // The UILabel already exists and is setup, just set the text
            pickerLabel.text = "Some text"
            
            return pickerLabel
        } else {
            // The UILabel doesn't exist, we have to create it and do the setup for font and textAlignment
            let pickerLabel = UILabel()
            
            pickerLabel.font = UIFont.boldSystemFont(ofSize: 18)
            pickerLabel.textAlignment = NSTextAlignment.center // By default the text is left aligned
            
            pickerLabel.text = "Some text"
            
            return pickerLabel
        }
    }