Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the Font size in UIPickerView?

I have one UIPickerView. This is having nearly 200 items, each items has long texts, so, i want to resize the UIPickerView's font size. How can i change it? It is possible? Can any one help me? Thanks !

Yuva.M

like image 273
Yuvaraj.M Avatar asked Aug 25 '11 05:08

Yuvaraj.M


People also ask

How do I change the pickerView font in Swift?

Fill your Font name, Color, Size & Data Array with appropriate values. func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { var pickerLabel: UILabel? = (view as? UILabel) if pickerLabel == nil { pickerLabel = UILabel() pickerLabel?.

How do I change font size in editor?

On the menu bar, choose Tools > Options. In the options list, choose Environment > Fonts and Colors. In Show settings for list, select Text Editor. Modify the Font and Size options to change the font and text size for the editor.


10 Answers

You need to implement pickerView:viewForRow:forComponent:reusingView: method in picker's delegate

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* tView = (UILabel*)view;
    if (!tView){
        tView = [[UILabel alloc] init];
            // Setup label properties - frame, font, colors etc
            ...
    }
    // Fill the label text here
    ...
    return tView;
}
like image 66
Nitish Avatar answered Oct 02 '22 02:10

Nitish


Update in Swift for iOS8, you can add this to your delegate:

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

    var pickerLabel = view as? UILabel;

    if (pickerLabel == nil)
    {
        pickerLabel = UILabel()

        pickerLabel?.font = UIFont(name: "Montserrat", size: 16)
        pickerLabel?.textAlignment = NSTextAlignment.Center
    }

    pickerLabel?.text = fetchLabelForRowNumber(row)

    return pickerLabel!;
}
like image 25
bownie Avatar answered Oct 02 '22 03:10

bownie


Swift 4: to update @Alessandro Ornano answer and avoid Force_cast violation error:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    var label = UILabel()
    if let v = view as? UILabel { label = v }
    label.font = UIFont (name: "Helvetica Neue", size: 10)
    label.text =  dataArray[row]
    label.textAlignment = .center
    return label
}
like image 41
Grég Avatar answered Oct 02 '22 03:10

Grég



For Font adjustment of UIPickerView Rows

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;

    if (!pickerLabel)
    {
        pickerLabel = [[UILabel alloc] init];

        pickerLabel.font = [UIFont fontWithName:@"SourceSansPro-Semibold"                size:16];

        pickerLabel.textAlignment=NSTextAlignmentCenter;
    }
    [pickerLabel setText:[self.data objectAtIndex:row]];

    return pickerLabel;
}
like image 33
Sivasagar Palakurthy Avatar answered Oct 02 '22 02:10

Sivasagar Palakurthy


Try this, it should help:

  - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
//adjustsFontSizeToFitWidth property to YES
tView.minimumFontSize = 8.;
tView.adjustsFontSizeToFitWidth = YES;
}
// Fill the label text here
...
return tView;
}


// altro modo completo sembrerebbe...
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {

UILabel *pickerLabel = (UILabel *)view;

if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
}

[pickerLabel setText:[pickerDataArray objectAtIndex:row]];

return pickerLabel;

}
like image 36
Ann Avatar answered Oct 02 '22 03:10

Ann


Swift 4.x

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var label = UILabel()
        if let v = view {
            label = v as! UILabel
        }
        label.font = UIFont (name: "Helvetica Neue", size: 10)
        label.text =  dataArray[row]
        label.textAlignment = .center
        return label
    }
like image 45
Alessandro Ornano Avatar answered Oct 02 '22 03:10

Alessandro Ornano


Swift 3 | AUTOSHRINK

Set adjustsFontSizeToFitWidth=true and minimumScaleFactor=0.5

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

    var label: UILabel
    if let view = view as? UILabel { label = view }
    else { label = UILabel() }

    label.text = "My Picker Text"
    label.textAlignment = .center
    label.font = UIFont.boldSystemFont(ofSize: 20)
    label.adjustsFontSizeToFitWidth = true
    label.minimumScaleFactor = 0.5

    return label
}
like image 38
Derek Soike Avatar answered Oct 02 '22 02:10

Derek Soike


Clean Swift solution without force unwrapping

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let label = (view as? UILabel) ?? UILabel()
    
    // setup label
    
    return label
}
like image 26
Walter White Avatar answered Oct 02 '22 04:10

Walter White


For Objective c

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        [pickerLabel setFont:[UIFont fontWithName:LATO_REGULAR_FONT size:SIZE_SEMIBOLD_FONT]];
        pickerLabel.textColor = primaryTextColor;
        pickerLabel.textAlignment = NSTextAlignmentCenter;


    }
    // Fill the label text here
    pickerLabel.text = self.dataSourceArray[row];

    return pickerLabel;
}

For Swift 2.3

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

        var label = view as! UILabel!
        if label == nil {
            label = UILabel()
        }

        label.font = LATO_REGULAR_FONT_17
        label.text =  dataArray[row] as? String
        label.textAlignment = .Center
        return label

    }
like image 27
Nischal Hada Avatar answered Oct 02 '22 02:10

Nischal Hada


Readable Swift version without ! or unnecessary reconfigurations

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let label: UILabel = (view as? UILabel) ?? {
        let label: UILabel = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textAlignment = .center
        return label
    }()
    label.text = self.pickerView(pickerView, titleForRow: row, forComponent: component)
    return label
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    items[row].title
}
like image 20
mmackh Avatar answered Oct 02 '22 04:10

mmackh