gpt4 book ai didi

iphone - 如何在选择器 View iOS 中设置文本对齐方式

转载 作者:可可西里 更新时间:2023-11-01 06:23:24 25 4
gpt4 key购买 nike

我需要在选取器 View 中设置文本的对齐方式,但我的解决方案不起作用。到目前为止,这是我的代码。我使用数组在选择器 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];
}

但是,它在显示值时工作正常。但是当我将文本居中对齐而不是显示文本(选择器 View 上的值)时,选择器 View 变为空。这是我如何在中心进行对齐的代码。

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

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

谢谢。

最佳答案

我完成了在 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

关于iphone - 如何在选择器 View iOS 中设置文本对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17967217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com