当前位置:  开发笔记 > 编程语言 > 正文

右对齐UIPickerView中的文本

如何解决《右对齐UIPickerView中的文本》经验,为你挑选了1个好方法。

如何正确对齐文本UIPickerView?我试图UILabel为行视图制作自定义s,但由于某种原因,没有任何东西出现,右对齐或其他.这是我写的:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setText:[NSString stringWithFormat:@"row %d", row]];
    [label setTextAlignment:UITextAlignmentRight];
    return [label autorelease];
}

如果有人想知道,我使用,CGRectZero因为我在UICatalog示例中看到了它.



1> Vipul Parmar..:

我在picker和两个数组中选择了两个组件来设置特定组件中行的标题.

下面的代码将在中心显示pickerdata,其中包含选择器的默认字体和fontsize.它将给出精确的pickerdata显示行为以及pickerdata的中心对齐.

这里,

NSArray *component1Array=[NSArray arrayWithObjects:@"0 lbs",@"1 lbs",@"2 lbs",@"3 lbs",@"4 lbs",@"5 lbs",nil];

NSArray *component2Array=[NSArray arrayWithObjects:@"0.00 oz",@"0.25 oz",@"0.50 oz",@"0.75 oz",@"1.00 oz",nil];

     - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
        {
             //I have taken two components thats why I have set frame of my "label" accordingly. you can set the frame of the label depends on number of components you have... 

             UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 145, 45)];

             //For right alignment of text,You can set the UITextAlignmentRight of the label.  
             //No need to set alignment to UITextAlignmentLeft because it is defaulted to picker data display behavior.

             [label setTextAlignment:UITextAlignmentCenter];
             label.opaque=NO;
             label.backgroundColor=[UIColor clearColor];
             label.textColor = [UIColor blackColor];
             UIFont *font = [UIFont boldSystemFontOfSize:20];
             label.font = font;
             if(component == 0)
             {
                 [label setText:[NSString stringWithFormat:@"%@",[component1Array objectAtIndex:row]]];
             }
             else if(component == 1)
             {
                 [label setText:[NSString stringWithFormat:@"%@", [component2Array objectAtIndex:row]]];
             }
             return [label autorelease];
        }

如果你使用上面的方法,你应该在下面提到UIPickerView委托方法评论...

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

上面示例代码的输出如下所示

替代文字

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有