2

I am trying to center and change the size of the text inside my UIPicker but am not sure how... I think it might happen in one of these methods, most likely the first...

- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [[NSString stringWithFormat:@"%x",row ] uppercaseString]; //Sets picker options as an Uppercase Hex value
}


#pragma mark UIPickerViewDataSource methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
{
    return 5; //5 columns in the picker
}

- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
    return 16;
}

Dose anyone one know how this can be done? I have looked around but the documentation is fairly thin...

C.Johns
  • 10,185
  • 20
  • 102
  • 156

2 Answers2

2

Johns,

As in Table Views, you can't change the font attributes of the default title that you usually set in the titleForRow delegate method, instead you have to use the following method:

- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component

Here you can create an UIView with a custom UILabel and set the Label's font size, color and other attributes.

This method is described in Apple's UIPickerView Class Reference

Hope this help you!

Paul N
  • 1,901
  • 1
  • 22
  • 32
  • wow... thats a real letdown.. you would think apple would cover this sort of thing in that titleForRow delegate method. – C.Johns Jul 13 '11 at 03:17
0

Now with iOS 6 you can use NSAttributedStrings to customize the title text (though I don't think this lets you easily center the text).

A new UIPickerViewDelegate method lets you return an attributed string instead of a plain vanilla string:

(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component

The NSAttributedString UIKit additions page has a list of all the character attributes you can control: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/uid/TP40011688

daver
  • 416
  • 4
  • 6