开发者

How to change the size of the characters inside a UIPicker

开发者 https://www.devze.com 2023-03-20 02:11 出处:网络
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...

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 opt开发者_StackOverflowions 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...


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号