如何在iOS

时间:2016-07-14 23:51:16

标签: ios objective-c

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

代码:

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

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

}

有什么想法吗?

3 个答案:

答案 0 :(得分:10)

有一种委托方法可以通过它获得所需的输出。

<强>目标-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
}

希望这会对你有所帮助:)。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

有委托方法:

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

}