Change font size of pickerview

Asked

Viewed 256 times

0

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

inserir a descrição da imagem aqui

As you can see right away the first item of the two lists does not appear the whole name. I guess because of the size.
On the left side the full name is: "BEER DA BOA"
On the right side your items are: "BEER DA BOA 01", "BEER DA BOA 02"
I wonder if you have how to change the font size of the pickerview or if you have the full description of the items to be visible.
I tried to use the code below, but despite changing the font COLOR, 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
    }

Thank you for your attention..
ps1 : If anyone Edit this post, please do NOT take words, as the same, including thanks, are to form a whole.

1 answer

1

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 a Uilabel instance so you can change the font the way you want using the property pickerLabel!. attributedText, 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
     }
 }
  • Good afternoon Edson <br> As I always say to everyone, you have to ask who KNOWS :) <br> Perfect your answer.. Now I can see all the items on the left and right side... I felt very MALE :)<br> My level in Swift programming is EASE-DRINKING... I still have a lot of time ahead. <br> My thanks so much for your help :) Hugs

  • Rsrsrsrs, when we are learning it’s like that, no one is born knowing anything, but over time and as you need to use things you will learn.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.