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.
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
Try this:
label.textAlignment = NSTextAlignmentCenter;
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..