UIPickerView Font - iphone

Does anyone know if it is possible to change the font and/or color for individual UIPickerView Items?

- (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;
}

Yes, it is possible. To do that you need to implement pickerView:viewForRow:forComponent:reusingView: method in picker's delegate - create UILabel instance there and setup it with whatever attributes you want.

For this, I would simply go with attributed strings.
Implement this method is your delegate:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView
attributedTitleForRow:(NSInteger)row
forComponent:(NSInteger)component

Related

Set font as per width

Hello friends I am displaying text in UIPickerview row. But when the label size is too big then Picker row displays ... at the end of the label.
I want to display whole title in row of Picker instead of this. Can i set the font size automatically as per the title width in pickerview.
You need to implement following method:
- (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;
}
Make your controller a UIPickerViewDelegate and UIPickerViewDataSource, then you can use this routine to make any custom change you want for your picker row:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//make your own view here, it can be a UILabel if you want, or other kind of view.
//You can give it any size font you need.
}

how to add picker view programmiticaly for list of image names

Im new to iphone development.In my application i want to add image picker for list of image names.here i added picker view and i dont no how to add list of image names programmiticaly in iphone with out using IB.
can any one plz give me code for displaying image names programmiticaly in pickerview.
Thankyou in advance.
#user549767 the answer of dks1725 is correct way to give images to your pickerview.....but in order to add pickerView programatically follow the below link....
UIPickerview in iphone
in viewDidLoad method make an array of your image names like below
-(void)viewDidLoad
{
arr=[[NSArray alloc] initWithObjects:#"image_name1",#"image_name2",#"image_name3",nil];
}
Now in picker view's delegate method write below code
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arr count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arr objectAtIndex:row];
}

iphone uipickerview with images

it's possible add as item in the uipickerview some images/icons instead of text?
thanks
yes it is absolutely possible. u need to handle pickerView delegate method
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
here you simply return custom view(could be anything UIImage UILabel)
and set userInteractionEnable property to no for customize view..

UIPicker With View + View Tapped != Selected Row

Hey! I have a UIPicker, and I am using the function:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
But, when you tap one of the views returned by this function, the tapped view does not become highlighed, and selected if the touch is released. How can I fix that?
Make use of this property,
pickerView.showsSelectionIndicator = YES;
at initialization.

Dynamically Add values to UIPickerView at run time

How can you dynamically add values to UIPickerView at runtime.
I'm using the following code to populate a UIPickerView statically. Need to add
values dynamically at run time, for e.g. Three, Four etc.
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *title = nil;
if(row==0){
title = #"One";
}
if(row==1){
title = #"Two";
}
If you had an array of the titles, you could use something like:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [titles objectAtIndex:row];
}
The titles will be refreshed when you call [pickerView reloadAllComponents] (or reloadComponent: if you have more than one column and only want to refresh one of them).
Hey I had a problem with UIPickerView..... when I call reloadAllComponents it wasn't calling widthForComponent, but it was calling other delegate methods such as titleForRow.... I have no idea why this is, perhaps apple doesn't want the width changing dynamically? anyway it was on another screen so I didn't have the animation/no-animation issue. I found that by resetting the delegate of the picker, I could get widthForComponent to be called again!
i.e.
picker.delegate = view;
where view implements UIPickerViewDelegate.... hope this helps someone out there!