Jarich
Jarich

Reputation: 337

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.

Upvotes: 3

Views: 10680

Answers (4)

Vikram Pote
Vikram Pote

Reputation: 5579

For swift 3 we can use

label.textAlignment = NSTextAlignment.center

Upvotes: 2

Hardik rami
Hardik rami

Reputation: 341

Try this:

label.textAlignment = NSTextAlignmentCenter;

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20551

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

enter image description hereenter image description here

Upvotes: 1

Nitin Gohel
Nitin Gohel

Reputation: 49730

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

enter image description here

Upvotes: 6

Related Questions