JBelbute
JBelbute

Reputation: 351

Swift uipickerview font

I'm trying to change the font of the rows in a UIPickerView and using the following code. No matter what I do I am getting errors and have exhausted about every option. I'm almost at the point where I think it is an Xcode defect. Any ideas

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

    let fontAttributes = [NSFontAttributeName: UIFont(name: "Verdana", size: 5.0)]

    if (component == FOOD) {

        let title = NSMutableAttributedString(string: foodArray[row], attributes: fontAttributes)
        return title
    }
    else {
        let title = NSMutableAttributedString(string: foodArray[row], attributes: fontAttributes)
        return title

   }
}

Upvotes: 2

Views: 3848

Answers (2)

JBelbute
JBelbute

Reputation: 351

After an exchange with Apple I have code that actually works here is an example. What you have to do is implement the viewForRow function, here is a working example

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

    var myString = view as UILabel!

    var myStringLength = 0

    if( view == nil) {
        myString = UILabel()
    }
    if (component == FOOD) {
        myString.textAlignment = .Center

        let rowText = foodArray[row]

        var attributedRowText = NSMutableAttributedString(string: rowText)
        var attributedRowTextLength = attributedRowText.length

        attributedRowText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location: 0, length: attributedRowTextLength))

        attributedRowText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 16.0)!, range: NSRange(location: 0 ,length:attributedRowTextLength))

        myString!.attributedText = attributedRowText

        return myString
    }
    else {
        myString.textAlignment = .Center

        let rowText = prepArray[row]

        var attributedRowText = NSMutableAttributedString(string: rowText)
        var attributedRowTextLength = attributedRowText.length

        attributedRowText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location: 0, length: attributedRowTextLength))

        attributedRowText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 16.0)!, range: NSRange(location: 0 ,length:attributedRowTextLength))

        myString!.attributedText = attributedRowText

        return myString

    }
}

Upvotes: 2

Andrew
Andrew

Reputation: 2467

This is a case of poor error messages by the compiler, since Swift is still a new language.

I'm assuming foodArray is of type [NSObject], or at least something other than [String]? Option-click it in Xcode to see its type if you didn't explicitly set it yourself.

Try declaring it as an array of strings:

let foodArray: [String] = // …

or

let foodArray = ["inferred", "as", "[String]", "from", "its", "contents"]

and if that's not feasible, retrieve your elements as Strings:

let title = NSMutableAttributedString(string: foodArray[row] as String, attributes: fontAttributes)


It might not help you in this case, but when I see strange error messages like this, I've found that the easiest way to go about isolating the issue is to temporarily avoid one-liners by declaring every argument individually before passing them into the function. That strategy has helped me a lot with UIView.animateWithDuration.

Upvotes: 1

Related Questions