Change font size of pickerview

0

Good afternoon. 09/05/2017
I have the following pickerview:

As you can see immediately the first item of the two lists does not appear the whole name. I think because of the size. On the left side the full name is: "BEER OF THE BOA"
On the right side your items are: "BEER OF GOOD 01", "BEER OF GOOD 02"
I wonder if you can change the font size of the pickerview or if you have how the whole description of the items are visible . I tried to use the code below, but in spite of changing the font color, it does not change the font size.

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?
    {// MUDA A COR DAS LINHAS
        var myTitle = NSAttributedString();
        if component == 0
        {
        let titleData = vLocal[row] as! Bebida_Class;

        myTitle = NSAttributedString(string: titleData.bebida_descricao,
                                     attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 10.0)!,
                                     NSForegroundColorAttributeName:UIColor.blue])
        }
        if component == 1
        {   
           etc....         
        }
        return myTitle
    }

Thanks in advance for your attention. ps1: If someone edits this post, please DO NOT take away words, because the same ones, including thank you, are to form a whole.

    
asked by anonymous 09.05.2017 / 21:35

1 answer

0

In this case it is best to use the following UIPickerViewDelegate method:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    var pickerLabel = view as? UILabel

    if (pickerLabel == nil){
        pickerLabel = UILabel()
        pickerLabel!.adjustsFontSizeToFitWidth = true
    }
    if component == 0 {
        pickerLabel!.text = cervejas[row]
    }else {
        pickerLabel!.text = descricao[row]
    }

    return pickerLabel!
}

Note that you return an instance of UILabel so you can change the font the way you want by using the pickerLabel! .attributedText property, if you need to change the width of each Component use the following delegate method:

  func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
     if component == 0 {
        return 50
     }else {
        return 100
     }
 }
    
10.05.2017 / 14:37