Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the text size and component width of a UIPickerView?

I have the following code to create a UIPickerView:

pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f, 416.0f - height, 320.0f, height)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[pickerView setSoundsEnabled:YES];

I would like to change the component widths and change the text size in each component. Is it possible to do this?

Thanks!

like image 844
rksprst Avatar asked Nov 02 '08 05:11

rksprst


People also ask

How do I change the font size in UIPickerView?

Try changing yourPickerView. frame. size. width or setting the size via the Interface Builder.

How do I change font size in Swift picker view?

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?.


1 Answers

You can change the width by an appropriate delegate method

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    switch(component) {
        case 0: return 22;
        case 1: return 44;
        case 2: return 88;
        default: return 22;
    }

    //NOT REACHED
    return 22;
}

As for a custom text size, you can use the delegate to return custom views with whatever sized text you want:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
        UILabel *retval = (id)view;
        if (!retval) {
            retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
        }

        retval.text = @"Demo";
        retval.font = [UIFont systemFontOfSize:22];
        return retval;
}

Of course you will need modify these to have appropriate values for your app, but it should get you where you need to go.

like image 102
Louis Gerbarg Avatar answered Oct 11 '22 00:10

Louis Gerbarg