如何在iOS中将UIPickerView文本颜色改为白色

时间:2022-05-06 00:09:07

I was wondering if anyone knew how to change the UIPickerView text colour to white. My code is below:

我想知道是否有人知道如何将UIPickerView文本颜色改为白色。我的代码如下:

Code:

代码:

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

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

}

Any idea?

任何想法?

3 个解决方案

#1


3  

Use this code

使用这个代码

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

#2


10  

There is one delegate method through which you can achieve desired output.

有一个委托方法,您可以通过它实现所需的输出。

Objective-C:

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;

}

Swift:

迅速:

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
}

Hope this will help you :)

希望这能对你有所帮助。

#3


0  

There is a delegate method:

有委托方法:

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

}

#1


3  

Use this code

使用这个代码

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

#2


10  

There is one delegate method through which you can achieve desired output.

有一个委托方法,您可以通过它实现所需的输出。

Objective-C:

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;

}

Swift:

迅速:

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
}

Hope this will help you :)

希望这能对你有所帮助。

#3


0  

There is a delegate method:

有委托方法:

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

}