How do I change the color of the text in a UIPickerView under iOS 7?

后端 未结 8 902
花落未央
花落未央 2020-11-30 19:39

I\'m aware of the pickerView:viewForRow:forComponent:reusingView method, but when using the view it passes in reusingView: how do I ch

相关标签:
8条回答
  • 2020-11-30 20:00

    in Xamarin, override the UIPickerModelView method 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;
    }
    
    0 讨论(0)
  • 2020-11-30 20:04

    I ran into the same problem with a pickerView using two components. My solution is similar to above with a few modifications. Because I am using two components it is necessary to pull from two different arrays.

    - (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;
    }
    
    0 讨论(0)
  • 2020-11-30 20:04
    - (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 讨论(0)
  • 2020-11-30 20:08
    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 讨论(0)
  • 2020-11-30 20:18

    Original post here: 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 讨论(0)
  • 2020-11-30 20:19

    Swift 4 (Update to the accepted answer)

    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 讨论(0)
提交回复
热议问题