How to Change the Color of the Text in a Uipickerview Under iOS 7

How do I change the color of the text in a UIPickerView under iOS 7?

There is a function in the delegate method that is more elegant:

Objective-C:

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

If you want to change the selection bar colors as well, I found that I had to add 2 separate UIViews to the view containing the UIPickerView, spaced 35 pts apart for a picker height of 180.

Swift 3:

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

let string = "myString"
return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white])
}

Swift 4:

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

let string = "myString"
return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}

Swift 4.2:

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

let string = "myString"
return NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}

Remember when you use the method: You don't need to implement titleForRowInComponent() as it is never called when using attributedTitleForRow().

How do I change the font color of UIPickerView and UIDatePicker?

you need to write this line to change date picker text color,

 self.datePicker.setValue(UIColor.white, forKeyPath: "textColor")

It will change textcolor of UIDatePicker

And For PickeView, You have to set attributed string like,

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

return NSAttributedString(string: "Your Text", attributes: [NSForegroundColorAttributeName:UIColor.blue])
}

Change Color of UIPickerView Text

  • Alternatively you may use the below delegate also and define a UILabel to set your preferred color for the text

    – pickerView:viewForRow:forComponent:reusingView:

UIPickerView Text Color Change In This Implementation

Add this method may be help you.

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSAttributedString *attString = nil;
NSString *title = @"";

if (component == SIZE)
title = [arraySize objectAtIndex:row];
if (component == MEASURE)
title = [arrayMeasure objectAtIndex:row];
attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];

return attString;

}

How do I change the text color of UIPickerView with multiple components in Swift?

The following pickerView:attributedTitleForRow:forComponent: method implementation should help you:

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let attributedString = NSAttributedString(string: "some string", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
return attributedString
}

Update

If you want to use attributedString in multiple if or switch statements, the following UIViewController subClass example will help you:

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var picker: UIPickerView!

let arrayOne = ["One", "Two", "Three", "Four", "Five", "Six"]
let arrayTwo = ["Un", "Deux", "Trois", "Quatre", "Cinq", "Six"]
let arrayThree = [1, 2, 3, 4, 5, 6]

override func viewDidLoad() {
super.viewDidLoad()

picker.delegate = self
picker.dataSource = self
}

func numberOfComponentsInPickerView(_: UIPickerView) -> Int {
return 3
}

func pickerView(_: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return arrayOne.count
case 1:
return arrayTwo.count
case 2:
return arrayThree.count
default:
return NSNotFound
}
}

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

switch component {
case 0:
attributedString = NSAttributedString(string: arrayOne[row], attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
case 1:
attributedString = NSAttributedString(string: arrayTwo[row], attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
case 2:
attributedString = NSAttributedString(string: toString(arrayThree[row]), attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
default:
attributedString = nil
}

return attributedString
}

func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch component {
case 0:
println(arrayOne[row])
case 1:
println(arrayTwo[row])
case 2:
println(arrayThree[row])
default:
break
}
}

}

How to change UIPickerView text colour to white in iOS

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

How can I change text font in UIPickerView in iOS 7?

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel* tView = (UILabel*)view;
if (!tView)
{
tView = [[UILabel alloc] init];
[tView setFont:[UIFont fontWithName:@"Helvetica" size:14]];
//[tView setTextAlignment:UITextAlignmentLeft];
tView.numberOfLines=3;
}
// Fill the label text here
tView.text=[wishvalues objectAtIndex:row];
return tView;
}


Related Topics



Leave a reply



Submit