2013-10-07 52 views
98

我知道pickerView:viewForRow:forComponent:reusingView方法,但是当使用view时,它会通过reusingView:如何将其更改为使用不同的文本颜色?如果我使用view.backgroundColor = [UIColor whiteColor];,则不再显示任何视图。如何更改iOS 7下UIPickerView中文本的颜色?

+0

的可能重复(http://stackoverflow.com/questions/18807940/can-i -change-the-font-color-of-date-date-picker -in-ios7) –

+1

@AaronBrager。在这个问题之后,不要重复你提供的链接。 –

回答

273

有一个在委托方法是更优雅的函数:

目的-C:

- (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; 
} 

如果你也想改变选择栏的颜色,我发现我必须添加2个单独的UIViews到包含012的视图,间隔开的35点为180

夫特3的拾取器高度:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 

    let string = "myString" 
    return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white]) 
} 

夫特4:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 

    let string = "myString" 
    return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]) 
} 

记住当使用方法:你不不需要执行titleForRowInComponent(),因为在使用attributedTitleForRow()时从不会调用它。

+2

令人惊叹!这是最好的,应该是选定的正确答案。谢谢。 – user1949873

+0

迄今为止,这是最好的答案,因为它也适用于具有多个组件的选取器视图。 – PKCLsoft

+0

不错的工作。我为使用数组的人添加了一个编辑到你的答案的编辑。如果这个问题让我知道。 – Rick

27

原始讯息[这里](can I change the font color of the datePicker in iOS7?

- (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; 
} 
+0

该解决方案适用于包含单个组件的pickerview。如果您有超过1个,标签将与我所能看到的重叠。 – PKCLsoft

2

我遇到了同样的问题,pickerView使用两个组件。我的解决方案与上述类似,只需进行一些修改。因为我使用两个组件,所以需要从两个不同的数组中抽取数据。

- (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; 
} 
18
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] 

screenshot

+0

请解释你的答案 – Gwenc37

+10

它的工作在一半,cus文本是黑色的,但是当你在选择器上滚动时,他会变成白色 – Shial

+0

这应该被标记为正确的答案! –

7
在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; 
} 
1
- (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; 
} 
0

夫特4(更新至接受的答案)

extension MyViewController: UIPickerViewDelegate{ 
    } 

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { 
     return NSAttributedString(string: "your-title-goes-here", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]) 
    } 
} 
0

这将工作:[?我能更改日期选择在iOS7的字体颜色]

- (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; 
} 

// total Components 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

// total rows In Component 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: 
(NSInteger)component 
{ 
    return 6; 
} 
+0

看起来像这是一个答案的复制/粘贴。它有什么不同? – Sunil

相关问题