2016-07-14 9 views
3

UIPickerViewのテキストの色を白に変更する方法を知っている人がいるかと思います。私のコードは以下の通りです:iOSでUIPickerViewのテキストの色を白に変更する方法

コード:

(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 

    [self.pickerView setValue:[UIColor whiteColor] forKey:@"textColor"]; 

} 

任意のアイデア?

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

} 
+0

- (UIViewの*)pickerView:(UIPickerView *)pickerViewをviewForRow:(NSInteger)row forComponent:(NSInteger)コンポーネントreusingView:(UIView *)ビュー { UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0、0、pickerView.frame.size.width、44)]; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor WhiteColor]; label.font = [UIFont fontWithName:@ "HelveticaNeue-Bold" size:18]; label.text = [NSString stringWithFormat:@ "%c"、65 + row]; // ASCII 65は "A" リターンラベルです。 } ありがとう – Nimmy

答えて

3

使用このコード

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    UILabel *tView = (UILabel*)view; 
    if (!tView) 
    { 
     tView = [[UILabel alloc] init]; 
     [tView setTextColor:[UIColor whiteColor]]; 
     [tView setFont:[UIFont fontWithName:@"font-name-here" size:15]]; 
     [tView setTextAlignment:NSTextAlignmentCenter]; 
    } 
    // Fill the label text here 
    return tView; 
} 
0

は、デリゲートメソッドがあります。

のObjective-C:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSString *strTitle = @"YourTitle"; 
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:strTitle attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; 

    return attString; 

} 

スウィフト:

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

    let strTitle = "YourTitle" 
    let attString = NSAttributedString(string: strTitle, attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()]) 
    return attString 
} 

希望これはあなたを助ける:)

10

あなたが所望の出力を達成することができ、それを通して1つのデリゲートメソッドがあります:

関連する問題