How I change text in UIPickerView from left to right - iphone

How I can change text in UIPickerView from leftside to rightsid.
for example if we suggest this is pickerview. I mean the text start from right side.

Instead of using the titleForRow delegate method of your picker view you should use viewForRow- that way you can return a UILabel that uses right justified text. If you just use the titleForRow method you don't have any control over how the text display, but if you use the latter you can customise it to your heart's content.

#lxt answer is right, but if he/she excuse me I'll put the full code
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *lblPickerTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 30)] autorelease];
lblPickerTitle.text = [pickerArray objectAtIndex:row];
lblPickerTitle.font = [UIFont fontWithName:#"Arial" size:20];
lblPickerTitle.textAlignment = UITextAlignmentRight;
return lblPickerTitle;
}

Related

How to set the text alignment in picker view iOS

I need to set the alignment of the text in picker view but my solutions doesn't work.
This is my code so far. I used array to store values on the picker view.
- (void)viewDidLoad{
[super viewDidLoad];
self.source = [[NSMutableArray alloc]
initWithObjects:#" 5 Minutes", #"10 Minutes", #"15 Minutes", #"20 Minutes",#"25 Minutes",#"30 Minutes",#"35 Minutes",#"40 Minutes",#"45 Minutes",#"50 Minutes",#"55 Minutes",#"60 Minutes", nil];
self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 275.0, 320.0, 120.0)];
self.picker.delegate = self;
self.picker.dataSource = self;
[self.view addSubview:self.picker];
[self.picker selectRow:2 inComponent:0 animated:YES];}
then,
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [source objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [source count];
}
However, it works fine when it displays the values. But when I align the text at the center, instead of displaying the text(values on picker view), the picker view becomes empty. This is the code on how I've done the alignment at the center.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component
reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
//label.text = [NSString stringWithFormat:#"something here"];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
return label;
}
What might be the solution for this?
Please help me regarding this problem.
Thanks.
i done with this code for UIlabel text show in center of tableView like:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
if (component == 0) {
label.font=[UIFont boldSystemFontOfSize:22];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:#"%#", [source objectAtIndex:row]];
label.font=[UIFont boldSystemFontOfSize:22];
}
[label autorelease];
return label;
}
Code output is
For swift 3 we can use
label.textAlignment = NSTextAlignment.center
use this code instead of your code..
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *viewTemp = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 44)]autorelease];
[viewTemp setBackgroundColor:[UIColor clearColor]];
UILabel *lbl = [[[UILabel alloc]initWithFrame:CGRectMake(5, 0, 300, 37)]autorelease];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setFont:[UIFont boldSystemFontOfSize:22]];
lbl.text = #"Some Text";
[lbl setTextAlignment:UITextAlignmentCenter];
[viewTemp addSubview:lbl];
return viewTemp;
}
Here i return one UIView in which i add UILable see above code so your whole view display with UILable properly and also set backGroundColor of UIView is clear so its display layout of UIPickerView.
I also display star like text in this UIPickerView.
See images Bellow..
Try this:
label.textAlignment = NSTextAlignmentCenter;

UIPickerView pickerView:viewForRow:forComponent:reusingView: does not affect below iOS 5.0

My UIPickerView retrieves it's data using the pickerView:viewForRow:forComponent:reusingView: method.
For some reason the method doesn't affect on iOS 4.3 and below.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel* label = (UILabel*)view;
if (view == nil) {
label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
}
label.text = #"Text";
return label;
}
This guy has made an example where he states that the view should not be autoreleased. http://alisothegeek.com/2009/07/custom-uipickerview-text-formatting/

integers and uiimage in uipicker view

i currently have a uipicker with two components. The first contains images and works fine, the second however requires integer values.
Is this possible since the following callback method returns a UIView type not integer? Surely there must be a way around this?
-(UIView *)pickerView
any help would greatly be appreciated, many thanks
this?
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
switch (component) {
case 1:
{
UILabel *intvalue = view?(UILabel *)view:[[[UILabel alloc] initWithFrame:CGRectMake(0,0, 50, 40)] autorelease];
intvalue.text = [NSString stringWithFormat:#"%d",value];
intvalue.textAlignment = UITextAlignmentCenter;
intvalue.backgroundColor = [UIColor clearColor];
return intvalue;
break;
}'
}

Iphone Sdk: UIPickerView, possible to assign different font sizes to different components?

i was just wondering if there was a way to change the fontSize of a row in a uipickerview
for a specific component and not for all of them. Also the different components still have to be able to display different things and not the same.
Does anyone have a solution for this problem? Thanks in advance!
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lbl = (UILabel *)view;
// Reuse the label if possible...
if ((lbl == nil) || ([lbl class] != [UILabel class])) {
CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
lbl = [[[UILabel alloc] initWithFrame:frame] autorelease];
.... do what you like ...
}
lbl.textColor = [UIColor blackColor];
lbl.text = [self.<yourdata> objectAtIndex:row];
return lbl;
}
The calling parameters tell you which view, row and component you are setting. Check them and set the appropriate thing only if both the row and component match (compare and use flow control) what you want for that row and component.

How to align picker view title text on each row to the right?

How can I align the title of each row in a UIPickerview to the right?
Thanks in advance.
You can implement delegate's pickerView:viewForRow:forComponent:reusingView: method and return UILabel instance from it:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* label = (UILabel*)view;
if (view == nil){
label= [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
label.textAlignment = UITextAlignmentRight;
//Set other properties if you need like font, text color etc
...
}
label.text = [self textForRow:row forComponent:component];
return label;
}