要求是选中某一行的时候字体变化,代码如下
.h<UIPickerViewDataSource,UIPickerViewDelegate>
@property (assign,nonatomic)NSInteger selectedRow;
.m
-(void)viewDidLoad
{
[super viewDidLoad];
self.timeArray = [[NSArray alloc]initWithObjects:@"0",@"2",@"4",@"6",@"8",@"10",@"12", nil];
self.selectedRow = -1;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.timeArray count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *timeString = self.timeArray[row];
return timeString;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//获取对应列,对应行的数据
self.pickTimeString=self.timeArray[row];
self.selectedRow = row;
[self.picker selectRow:self.selectedRow inComponent:0 animated:YES];
[self.picker reloadComponent:0];
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel)
{
pickerLabel = [[UILabelalloc]init];
pickerLabel.minimumScaleFactor =0.5;
pickerLabel.adjustsFontSizeToFitWidth =YES;
[pickerLabel setTextAlignment:NSTextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColorclearColor]];
}
if (row ==self.selectedRow) {
NSString *selectString = [self.timeArrayobjectAtIndex:self.selectedRow];
NSDictionary *attributeDict =@{NSForegroundColorAttributeName : [UIColororangeColor]};
NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc]initWithString:selectStringattributes:attributeDict];
NSRange stringRange = {0,[attributedStringlength]};
[attributedStringaddAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle]range:stringRange];
pickerLabel.attributedText = attributedString;
}else{
pickerLabel.text=[selfpickerView:pickerViewtitleForRow:rowforComponent:component];
}
return pickerLabel;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 30;
}