talsharonts
talsharonts

Reputation: 277

How To Make UIPickerView components text to always fit?

Let's say you have a UIPickerView and you want your text to always fit the given space its component gets (No "..." for misfitted text).

How to pull this off?

Upvotes: 1

Views: 1486

Answers (1)

talsharonts
talsharonts

Reputation: 277

Simple solution:

Implement

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

in your picker delegate like this:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* tView = (UILabel*)view;
    if (!tView)
    {
        tView = [[UILabel alloc] init];

        tView.textAlignment = NSTextAlignmentCenter;

        tView.adjustsFontSizeToFitWidth = YES;
        // Setup label properties - frame, font, colors etc
    }

    tView.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    //Add any logic you want here

    return tView;
} 

This makes the labels to fit the text inside them perfectly.

Enjoy!

Upvotes: 6

Related Questions