2014-04-25 45 views
0

我試圖根據一定條件更改viewForRow中的文本顏色。當我按下按鈕時,視圖會變成不同的顏色,但我還想更改選取器中文本的顏色。我使用'viewForRow'是因爲我有一個自定義視圖。更改UIPickerView pickerView:viewForRow:基於一定條件的屬性

//adds subcategories to the wheel 
-(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 clearColor]; 


    if (!self.isBackgroundWhite)//Boolean that changes when the background changes 
    { 
     NSLog(@"Set white text"); 
     label.textColor = [UIColor whiteColor]; 

    }else{ 

     label.textColor = [UIColor blackColor]; 
    } 



    if (row == 1) { 
     label.text = [NSString stringWithFormat:@" %@",[self.servicesArray objectAtIndex:row]]; 
     label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 
    }else{ 
     label.text = [NSString stringWithFormat:@"  -%@",[self.servicesArray objectAtIndex:row] ]; 
     label.font = [UIFont fontWithName:kAppFont size:16]; 
    } 
     return label; 
} 

編輯:感謝@Rich我能夠發現我的問題,我只需要調用[self.pickerView reloadAllComponents];

+0

你只想改變文字顏色而沒有別的? – Rich

+0

@僅限yup文本顏色 – DevC

回答

2

如果你只是想更改文本顏色只需使用其他委託方法pickerView:attributedTitleForRow:forComponent:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 

    NSString *title; 
    UIFont *font; 
    if (row == 1) { 
     title = [NSString stringWithFormat:@" %@",[self.servicesArray objectAtIndex:row]]; 
     font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 
    } else{ 
     title = [NSString stringWithFormat:@"  -%@",[self.servicesArray objectAtIndex:row] ]; 
     font = [UIFont fontWithName:kAppFont size:16]; 
    } 

    UIColor *textColor; 
    if (self.isBackgroundWhite) { 
     textColor = [UIColor blackColor]; 
    } else { 
     NSLog(@"Set white text"); 
     textColor = [UIColor whiteColor]; 
    } 

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    paragraphStyle.alignment = NSTextAlignmentLeft; 

    NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor, 
              NSFontAttributeName : font, 
            NSParagraphStyleAttributeName : paragraphStyle}; 

    return [[NSAttributedString alloc] initWithString:title attributes:attributes]; 
} 

另外一張紙條改變isBackgroundWhite時,你應該叫[self.pickerView reloadAllComponents];。我將通過覆蓋backgroundWhite的setter來完成此操作,並且當布爾值更改時重新加載選取器視圖。

編輯:
似乎有在IOS 7「錯誤」(有意或無意)(在iOS 6正常工作)與設置在從pickerView:attributedTitleForRow:forComponent:方法返回NSAttributedStringUIFont。所以,雖然上面的文字顏色的工作字體不會改變。幸運的是,你可以用問題中的代碼來解決這個問題。有關更多信息,請參閱this answer

+0

我需要將所有標題左對齊而不是居中?有沒有辦法做到這一點? – DevC

+1

@Gman是的,你可以使用'NSParagraphStyleAttributeName',我會補充說。 – Rich

+0

@Gman檢出[''NSMutableParagraphStyle']](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMutableParagraphStyle_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableParagraphStyle)在'paragraphStyle'對象上設置更多選項:) – Rich

相關問題