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

3
我需要设置选择器视图中文本的对齐方式,但我的解决方案无效。 这是我迄今为止的代码。我使用数组来存储选择器视图上的值。
- (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;
}

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

对于UIPickerView,你需要使用titleForRow:方法或者viewForRow:方法。你不能同时使用这两个方法。 - Dinesh Raja
好的。如果我使用titleForRow方法,我在哪里可以放置'UiTextAlignmentCenter'以使UIPickerView居中对齐? - Jarich
你需要使用viewForRow:方法,并返回UITextAlignment属性为center的UILabel。或者你可以使用我在github上的DPickerView(http://dineshrajas.github.io/DPickerView/),它默认提供居中文本。 - Dinesh Raja
嗯,我现在明白了titleForRow和viewForRow方法之间的区别。我会像你建议的那样使用viewForRow。谢谢! - Jarich
4个回答

6
我已经完成了以下代码,实现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;
 }

代码输出结果为:

图片描述


1
非常感谢您 Nitin Gohel。它很好用!我已经接受了您的答案,但是现在我还没有声望值,无法投票支持。不久我会回来给您点赞的。谢谢! - Jarich

2

对于 Swift 3,我们可以使用

label.textAlignment = NSTextAlignment.center

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,其中我添加了 UILabel,请参见上面的代码,以便您的整个视图能够正确地显示带有 UILabel,并且将 UIView 的背景颜色设置为透明,以便其显示 UIPickerView 的布局。
我还在这个 UIPickerView 中显示星形文本。
请参见下面的图片。

enter image description hereenter image description here


你是如何创建带有评分星级的UIPickerView的? - andilabs
我只是在UIPickerView的代理方法viewForRow:中将rateView设置为UIView。 - Paras Joshi
是的,我刚刚在UIPickerView中添加了DYRateView.. :) - Paras Joshi

0

试试这个:

label.textAlignment = NSTextAlignmentCenter;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接