UIPickerView控制元件中自定義 及 改變當前選中的Item的字型顏色
#pragma mark -UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return @[@"今天",@"明天"].count;
}else{
return @[@"立即配送",@"19:30",@"19:00"].count;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
return @[@"今天",@"明天"][row];
}else{
return @[@"立即配送",@"19:30",@"19:00"][row];
}
}
//選擇相應列表
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
UILabel *piketLabel = (UILabel *)[pickerView viewForRow:row forComponent:component];
piketLabel.textColor = [UIColor redColor];
}
//重寫方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
//adjustsFontSizeToFitWidth property to YES
pickerLabel.adjustsFontSizeToFitWidth = YES;
[pickerLabel setTextAlignment:NSTextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
}
// Fill the label text here
pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
return pickerLabel;
}
#pragma mark - property
- (UIPickerView *)pickerView{
if (!_pickerView) {
_pickerView =[[UIPickerView alloc]initWithFrame:CGRectMake(0, 40,self.bounds.size.width, self.bounds.size.height - 40)];
_pickerView.delegate = self;
_pickerView.dataSource = self;
}
return _pickerView;
}