如何在选择器视图iOS中设置文本对齐方式

时间:2013-07-31 09:55:17

标签: iphone ios

我需要在选择器视图中设置文本的对齐方式,但我的解决方案不起作用。 到目前为止这是我的代码。我使用数组在选择器视图上存储值。

- (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;
}

这可能是什么解决方案? 请帮我解决这个问题。

感谢。

4 个答案:

答案 0 :(得分:6)

我使用此代码完成了在tableView中心的UIlabel文本显示: -

- (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;
 }

代码输出

enter image description here

答案 1 :(得分:2)

对于swift 3,我们可以使用

label.textAlignment = NSTextAlignment.center

答案 2 :(得分:1)

使用此代码代替您的代码..

-(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;
}

在这里,我返回一个UIView,其中我添加了UILable,请参阅上面的代码,以便您的整个视图正确显示UILable,同时设置UIView的backGroundColor是明确的显示UIPickerView的布局。

我还在此UIPickerView中显示明星文字。

见图片Bellow ..

enter image description here enter image description here

答案 3 :(得分:0)

试试这个:

label.textAlignment = NSTextAlignmentCenter;