Set font as per width - iphone

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

Related

Custom UIPickerView

I want to use custom picker view that will add a check sign in front of selected row.
I have used apple sample code for custom UIPickerView on the UICatalog example. I was able to add the check sign for a given row when creating the picker. But I failed to add it when the user rotate the wheel to select new row and also to remove it from the previously added row. Any help would be appreciated.
Thanks,
1) Create subclass of UIView that will represent row in picker. Define property, for example, isChecked, which will show/hide checkmark on this view
2) In the – pickerView:didSelectRow:inComponent: call – viewForRow:forComponent: for previously selected row, set isChecked=NO
3) call – viewForRow:forComponent: for currently selected row and set isChecked=YES;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
MyCustomRowView *prevRowView = [pickerView viewForRow:currentlySelectedRow forComponent:component];
prevRowView.isChecked = NO;
MyCustomRowView *currentRowView = [pickerView viewForRow:row forComponent:component];
currentRowView.isChecked = YES;
//then save currently selected row
currentlySelectedRow = row;
}
4) you also should check currently selected row when you is requested for it:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
....
//Create or reuse view
....
rowView.isChecked = (row == currentlySelectedRow);
}
As you are not providing any code, all we can give you is a general advise;
make sure the viewController showing your UIPickerView instance is inheritating the UIPickerViewDelegate-protocol
set the delegate of your UIPickerView towards this viewController; e.g. pickerView.delegate = self;
implement - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
within the above implementation, remove any checkmark previously added and add a new one to the row selected.

UIPickerView Font

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

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.