İOS 7 altında bir UIPickerView'daki metnin rengini nasıl değiştiririm?


117

Bildiğim pickerView:viewForRow:forComponent:reusingViewyöntemle ama kullanırken viewbunun içinde geçtiği reusingView:nasıl farklı bir metin rengi kullanmak değiştirebilirim? view.backgroundColor = [UIColor whiteColor];Görünümlerden hiçbirini kullanmazsam artık görünmüyor.



2
@AaronBrager. Que Bu kuyruktan sonra u tarafından sağlanan bağlantının kopyası değil istenir.
— Gajendra K Chauhan

Yanıtlar:


323

Temsilci yönteminde daha zarif bir işlev vardır:

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

Seçim çubuğu renklerini de değiştirmek isterseniz , 180'lik bir seçici yüksekliği için 35 pts aralıklı UIViewsgörüntüyü içeren görünüme 2 ayrı eklemek zorunda kaldım UIPickerView.

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])
}

Yöntemi ne zaman kullandığınızı unutmayın: titleForRowInComponent()Kullanırken asla çağrılmadığı için uygulamaya gerek yoktur attributedTitleForRow().


Birden fazla bileşene sahip seçici görünümler için de işe yaradığından, bu açık ara en iyi yanıttır.
— PKCLsoft

29

Buraya orijinal gönderi: iOS7'de datePicker'ın yazı tipi rengini değiştirebilir miyim?

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

Bu çözüm, tek bir bileşen içeren bir pickerview için iyi çalışıyor. 1'den fazlasına sahipseniz, etiketler benim görebildiklerimle çakışacaktır.
— PKCLsoft

23
  1. Film şeridine git
  2. PickerView'ı seçin
  3. Kimlik denetçisine gidin (3. sekme)
  4. Kullanıcı Tanımlı Çalışma Zamanı Özniteliği Ekle
  5. KeyPath = textColor
  6. Tür = Renk
  7. Değer = [Seçtiğiniz renk]

ekran görüntüsü


Lütfen cevabınızı açıklayın
— Gwenc37

11
bu yarım işe yarıyor, cus metin siyah ama seçiciye
— kaydırdığınızda

4
Yalnızca kaydırırsanız çalışır
— Chris Van Buskirk

Merhaba @ chris-van-buskirk [_thePrettyPicker selectRow: prettyIndex inComponent: 0 animasyonlu: YES];
— WINSergey

7

Xamarin'de, UIPickerModelView yöntemini GetAttributedTitle geçersiz kılın

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


2

İki bileşen kullanan bir pickerView ile aynı problemle karşılaştım . Benim çözümüm, birkaç değişiklikle yukarıdakine benzer. İki bileşen kullandığım için iki farklı diziden çekmem gerekiyor.

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

2

Swift 4 (Kabul edilen cevaba güncelleme)

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])
    }
}

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;
}
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.