如何更改iOS 7下UIPickerView中文本的颜色?
我知道pickerView:viewForRow:forComponent:reusingView
方法,但是当使用view
它传入reusingView:
我如何将其更改为使用不同的文本颜色? 如果我使用view.backgroundColor = [UIColor whiteColor];
没有任何意见显示了。
在委托方法中有一个更优雅的函数:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString *title = @"sample title"; NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; return attString; }
如果你想改变select栏的颜色,我发现我必须添加2个独立的UIViews到包含UIPickerView的视图,间隔35分,select器高度为180。
Swift 3的更新:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let string = "myString" return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white]) }
记住,当你使用方法:你不需要实现titleForRowInComponent()
因为它使用的时候是从不调用的。
原post[这里]( 我可以更改iOS7中的datePicker的字体颜色? )
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)]; label.backgroundColor = [UIColor grayColor]; label.textColor = [UIColor whiteColor]; label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; label.text = [NSString stringWithFormat:@" %d", row+1]; return label; } // number Of Components - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } // number Of Rows In Component - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component { return 6; }
1. Go to storyboard 2. Select PickerView 3. Go to Identity inspector (3rd tab) 4. Add User Defined Runtime Attribute 5. KeyPath = textColor 6. Type = Color 7. Value = [Color of you choice]
在Xamarin中,重写UIPickerModelView方法GetAttributedTitle
public override NSAttributedString GetAttributedTitle(UIPickerView picker, nint row, nint component) { // change text white string title = GetTitle (picker, row, component); // get current text from UIPickerViewModel::GetTitle NSAttributedString ns = new NSAttributedString (title, null, UIColor.White); // null = font, use current font return ns; }
我遇到了一个pickerView使用两个组件相同的问题。 我的解决scheme与上述类似,只需要进行一些修改。 因为我使用两个组件,所以需要从两个不同的数组中取出。
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *label = [[UILabel alloc] init]; label.backgroundColor = [UIColor blueColor]; label.textColor = [UIColor whiteColor]; label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; //WithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 60)]; if(component == 0) { label.text = [countryArray objectAtIndex:row]; } else { label.text = [cityArray objectAtIndex:row]; } return label; }
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel* pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 37)]; pickerLabel.text = @"text"; pickerLabel.textColor = [UIColor redColor]; return pickerLabel; }