Reputation: 57
I was wondering if anyone knew how to change the UIPickerView text colour to white. My code is below:
Code:
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
[self.pickerView setValue:[UIColor whiteColor] forKey:@"textColor"];
}
Any idea?
Upvotes: 4
Views: 5119
Reputation: 1895
There is one delegate method through which you can achieve desired output.
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 :)
Upvotes: 10
Reputation: 1698
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;
}
Upvotes: 0
Reputation: 8680
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;
}
Upvotes: 4