如何自定义UIPickerView中文本的大小和文本靠左或靠右显示?

时间:2024-01-12 11:26:02

需要重写UIPickerView中的

-(UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

方法

示例代码如下

 //自定义uipickerview中的文字大小
-(UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *myView=nil; myView=[[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, , )];
myView.textAlignment=NSTextAlignmentRight; if (pickerView==self.SourcePicker) {
myView.text=[_SourcePickerArray objectAtIndex:row]; }else{
myView.text=[_RoomPickerArray objectAtIndex:row];
}
myView.font=[UIFont systemFontOfSize:];
myView.textColor=[UIColor blackColor];
myView.backgroundColor=[UIColor clearColor];
return myView; }

另外注意,ios6之前调整文本靠左或者靠右对齐的代码为

mylabel.textAlignment= UITextAlignmentCenter;

在ios7以上版本需要改为

mylabel.textAlignment=NSTextAlignmentCenter;