我需要设置选择器视图中文本的对齐方式,但我的解决方案无效。
这是我迄今为止的代码。我使用数组来存储选择器视图上的值。
然后,
可能的解决方案是什么?请帮我解决这个问题。
谢谢。
- (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];}
然后,
- (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];
}
然而,当它显示数值时,它可以正常工作。但是当我将文本居中对齐时,选择器视图不会显示文本(即数值),而是变为空白。这是我如何在中心对齐时编写的代码:
- (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;
}
可能的解决方案是什么?请帮我解决这个问题。
谢谢。