我希望能够更改两行的文本颜色,我正在使用此代码:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
NSString *rowItem = [weight objectAtIndex: row];
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];
[lblRow setTextAlignment:UITextAlignmentCenter];
[lblRow setTextColor: [UIColor redColor]];
[lblRow setText:rowItem];
[lblRow setBackgroundColor:[UIColor clearColor]];
return lblRow;
}
它使UIPickerView中的两个组件具有相同的数据,但它会改变颜色。如何在不更改数据的情况下更改两个组件的颜色?
答案 0 :(得分:4)
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component ==0)
{
NSString *title = @"maulik";
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
return attString;
}
if(component ==1)
{
NSString *title = @"fenil";
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
return attString;
}
return 0;
}
答案 1 :(得分:-1)
您有两个参数row
和component
如果调整组件并获得所需数据,则必须使用
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
if (component == 0)
NSString *rowItem = [weight objectAtIndex: row];
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];
[lblRow setTextAlignment:UITextAlignmentCenter];
[lblRow setTextColor: [UIColor redColor]];
[lblRow setText:rowItem];
[lblRow setBackgroundColor:[UIColor clearColor]];
return lblRow;
} else {
// Adjust the second component
}
}