ฉันรู้pickerView:viewForRow:forComponent:reusingViewวิธีการนี้ แต่เมื่อใช้viewมันผ่านreusingView:ฉันจะเปลี่ยนเป็นสีข้อความอื่นได้อย่างไร ถ้าฉันview.backgroundColor = [UIColor whiteColor];ไม่ใช้มุมมองใด ๆ ปรากฏขึ้นอีกต่อไป
ฉันรู้pickerView:viewForRow:forComponent:reusingViewวิธีการนี้ แต่เมื่อใช้viewมันผ่านreusingView:ฉันจะเปลี่ยนเป็นสีข้อความอื่นได้อย่างไร ถ้าฉันview.backgroundColor = [UIColor whiteColor];ไม่ใช้มุมมองใด ๆ ปรากฏขึ้นอีกต่อไป
คำตอบ:
มีฟังก์ชั่นในวิธีการมอบหมายที่หรูหรากว่า:
Objective-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ในมุมมองที่มีUIPickerViewระยะห่าง 35 พอยต์สำหรับความสูงของตัวเลือก 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])
}Swift 4:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}Swift 4.2:
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}โปรดจำไว้ว่าเมื่อคุณใช้วิธีการ: คุณไม่จำเป็นที่จะใช้มันเป็นไม่เคยเรียกเมื่อใช้titleForRowInComponent()attributedTitleForRow()
โพสต์ต้นฉบับที่นี่: ฉันสามารถเปลี่ยนสีฟอนต์ของ datePicker ใน 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;
}
ใน 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.setValue(UIColor.yellow, forKeyPath: "textColor")ฉันพบปัญหาเดียวกันกับ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;
}Swift 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])
    }
}- (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;
}