How to Change Text Font in Uipickerview in iOS 7

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

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])
}

How to change the Font size in UIPickerView?

You need to implement pickerView:viewForRow:forComponent:reusingView: method in picker's delegate

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
}
// Fill the label text here
...
return tView;
}

How do I change the font size in a UIPickerView in Swift?

Try this for Swift 3.x:

Fill your Font name, Color, Size & Data Array with appropriate values.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var pickerLabel: UILabel? = (view as? UILabel)
if pickerLabel == nil {
pickerLabel = UILabel()
pickerLabel?.font = UIFont(name: "<Your Font Name>", size: <Font Size>)
pickerLabel?.textAlignment = .center
}
pickerLabel?.text = <Data Array>[row]
pickerLabel?.textColor = UIColor.blue

return pickerLabel!
}

EDIT:

For Multiple components, you can do something like this:

if component == 0 {
var label: UILabel? = (view as? UILabel)
label.text = <Your Arr>[row]
return label
}else {
return anotherLabel
}

Output:

Sample Image

Hope it helps!!!

How to change UIPickerView font size

NSAttributedString cannot be used to change font for UIPickerView.

You can change font by overriding pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView

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


Related Topics



Leave a reply



Submit