right align text in UIPickerView
-
05-09-2019 - |
Question
How can I right align text in a UIPickerView
? I tried to make custom UILabel
s for the row views, but for some reason, nothing is showing up, right-aligned or otherwise. Here's what I wrote:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setText:[NSString stringWithFormat:@"row %d", row]];
[label setTextAlignment:UITextAlignmentRight];
return [label autorelease];
}
In case anyone is wondering, I used CGRectZero
because I saw it in the UICatalog
example.
Solution
Your not seeing anything because of CGRectZero. You need to set a size in your case.
In the UICatalog, if your talking about how they used CGRectZero for the CustomView... well if you look at CustomView.m you will see they are actually disregarding the CGRectZero and setting a frame to a size in the initWithFrame:
OTHER TIPS
I have taken two components in picker and two arrays to set the title of the rows in a particular component.
Below code will display pickerdata in center with default font and fontsize of the picker. It will give exact pickerdata display behavior with center alignment of the pickerdata.
Here,
NSArray *component1Array=[NSArray arrayWithObjects:@"0 lbs",@"1 lbs",@"2 lbs",@"3 lbs",@"4 lbs",@"5 lbs",nil];
NSArray *component2Array=[NSArray arrayWithObjects:@"0.00 oz",@"0.25 oz",@"0.50 oz",@"0.75 oz",@"1.00 oz",nil];
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//I have taken two components thats why I have set frame of my "label" accordingly. you can set the frame of the label depends on number of components you have...
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 145, 45)];
//For right alignment of text,You can set the UITextAlignmentRight of the label.
//No need to set alignment to UITextAlignmentLeft because it is defaulted to picker data display behavior.
[label setTextAlignment:UITextAlignmentCenter];
label.opaque=NO;
label.backgroundColor=[UIColor clearColor];
label.textColor = [UIColor blackColor];
UIFont *font = [UIFont boldSystemFontOfSize:20];
label.font = font;
if(component == 0)
{
[label setText:[NSString stringWithFormat:@"%@",[component1Array objectAtIndex:row]]];
}
else if(component == 1)
{
[label setText:[NSString stringWithFormat:@"%@", [component2Array objectAtIndex:row]]];
}
return [label autorelease];
}
You should comment below mention UIPickerView delegate method if you are using above method...
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
The output of above sample code will look like below
In iOS 6, you can now return an NSAttributedString, which can contain text alignment attributes. I posted a short snippet on another related question here: https://stackoverflow.com/a/14035356/928963
Make sure you:
- Provide an explicit frame that is as high and wide as needed;
- Set the label's display opaqueness (via .opaque = YES or NO) and background color appropriately (you usually want NO and [UIColor clearColor] respectively).
- Set the font explicitly.