如何在Picker View iOS中设置文本对齐方式 [英] How to set the text alignment in picker view iOS

查看:267
本文介绍了如何在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];}

然后

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

谢谢.

推荐答案

我用这段UIlabel文本代码在tableView的中心显示,如:-

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

代码输出为

这篇关于如何在Picker View iOS中设置文本对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆