UIPickerView Row Color - iphone

Does anyone know how to change the color of a row (or row background) in the UIPickerView control from the iPhone SDK? Similiar to the below title for row, however I would also like to change the color of the row:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
Thank you.

Thanks Noah, that's exactly what I needed. I wanted to add the code here just in case anyone else needs (or wants to comment on :)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}

You can return an arbitrary view in the delegate's -pickerView:viewForRow:forComponent:reusingView: method, documented here.

I implemented the following based on Sean's answer:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
CGRect rowFrame = CGRectMake(00.0f, 0.0f, [pickerView viewForRow:row forComponent:component].frame.size.width, [pickerView viewForRow:row forComponent:component].frame.size.height);
UILabel *label = [[UILabel alloc] initWithFrame:rowFrame];
label.font = [UIFont boldSystemFontOfSize:18.0f];
// This is an array I pass to the picker in prepareForSegue:sender:
label.text = [self.values objectAtIndex:row];
label.textAlignment = UITextAlignmentCenter;
// This is an array I pass to the picker in prepareForSegue:sender:
if ([self.backgroundColors count]) {
label.backgroundColor = [self.backgroundColors objectAtIndex:row];
// self.lightColors is an array I instantiate in viewDidLoad: self.lightColors = #[ [UIColor yellowColor], [UIColor greenColor], [UIColor whiteColor] ];
label.textColor = [self.lightColors containsObject:label.backgroundColor] ? [UIColor blackColor] : [UIColor whiteColor];
} else {
label.textColor = [UIColor blackColor];
}
return label;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
[labelSelected setTextColor:[UIColor redColor]];
}
And
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = (id)view;
if (!label){
label=[[UILabel alloc]init];
label.textAlignment = NSTextAlignmentCenter;
pickerView.backgroundColor=[UIColor whiteColor];
label.text=[self pickerView:pickerView titleForRow:row forComponent:component];
label.textColor=[UIColor grayColor];
}
return label;
}

Do this:
label.backgroundColor = [UIColor yourcolorColor];

Related

How to set the text alignment in picker view iOS

I need to set the alignment of the text in picker view but my solutions doesn't work.
This is my code so far. I used array to store values on the picker view.
- (void)viewDidLoad{
[super viewDidLoad];
self.source = [[NSMutableArray alloc]
initWithObjects:#" 5 Minutes", #"10 Minutes", #"15 Minutes", #"20 Minutes",#"25 Minutes",#"30 Minutes",#"35 Minutes",#"40 Minutes",#"45 Minutes",#"50 Minutes",#"55 Minutes",#"60 Minutes", nil];
self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 275.0, 320.0, 120.0)];
self.picker.delegate = self;
self.picker.dataSource = self;
[self.view addSubview:self.picker];
[self.picker selectRow:2 inComponent:0 animated:YES];}
then,
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [source objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [source count];
}
However, it works fine when it displays the values. But when I align the text at the center, instead of displaying the text(values on picker view), the picker view becomes empty. This is the code on how I've done the alignment at the center.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component
reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
//label.text = [NSString stringWithFormat:#"something here"];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
return label;
}
What might be the solution for this?
Please help me regarding this problem.
Thanks.
i done with this code for UIlabel text show in center of tableView like:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
if (component == 0) {
label.font=[UIFont boldSystemFontOfSize:22];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:#"%#", [source objectAtIndex:row]];
label.font=[UIFont boldSystemFontOfSize:22];
}
[label autorelease];
return label;
}
Code output is
For swift 3 we can use
label.textAlignment = NSTextAlignment.center
use this code instead of your code..
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *viewTemp = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 44)]autorelease];
[viewTemp setBackgroundColor:[UIColor clearColor]];
UILabel *lbl = [[[UILabel alloc]initWithFrame:CGRectMake(5, 0, 300, 37)]autorelease];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setFont:[UIFont boldSystemFontOfSize:22]];
lbl.text = #"Some Text";
[lbl setTextAlignment:UITextAlignmentCenter];
[viewTemp addSubview:lbl];
return viewTemp;
}
Here i return one UIView in which i add UILable see above code so your whole view display with UILable properly and also set backGroundColor of UIView is clear so its display layout of UIPickerView.
I also display star like text in this UIPickerView.
See images Bellow..
Try this:
label.textAlignment = NSTextAlignmentCenter;

Change specific row color uipicker

i have the following problem: I can change the text color of the rows inside the uipicker but not the text color of the specific row i want, the picker gets all confused and a lot of text labels become blue.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (UILabel*)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
retval.text = [arraySubSistemasDeSaude objectAtIndex:row];
if(pickerView == subSistemaSaudePicker) {
if ([retval.text isEqualToString:#"Seguros de saúde"]) {
NSLog(#"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
}
if ([retval.text isEqualToString:#"Sistemas de Saúde e Equiparados"]) {
NSLog(#"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
if ([retval.text isEqualToString:#"Seguradoras-Acidentes de Trabalho, viação/pessoais e vida"]) {
NSLog(#"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
}
if ([retval.text isEqualToString:#"Empresas, associações e outras entidades"]) {
NSLog(#"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
}
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
}
} else if(pickerView == horarioPicker) {
retval.text =[arrayHorarios objectAtIndex:row];
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
} else if(pickerView == examesPicker) {
retval.text =[arrayExames objectAtIndex:row];
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
} else if(pickerView == consultasPicker) {
retval.text =[arrayConsultas objectAtIndex:row];
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
}else {
assert(NO);
}
return retval;
}
Sorry for my bad english,i hope i made myself clear with the question, thanks in advance.
The problem is, you are reusing the view for each row and you are not setting the UILabel color back to black when it is not supposed to be blue.
Add this code...
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (UILabel*)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
[retval setTextColor:[UIColor blackColor]];
retval.text = [arraySubSistemasDeSaude objectAtIndex:row];
......
With this, each time it pulls up a reused view it will first set the color to black. Then the code will check for the ones you want that are blue and set them to blue. The way you were doing it, it was setting the blue color on some of them, then when you went to reuse them you were never resetting it to black.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
[pickerLabel setTextColor:[UIColor blackColor]];
[pickerLabel setText:#"MyTest"];
}
return pickerLabel;
}

How to change text color inside uipicker?

How to change the text color inside picker with various colors. For Example:
Use pickerView:viewForRow, and return a UILabel that has the color you want
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] init];
label.text = #"Row";
label.textColor = [UIColor greenColor];
return label;
}
Try this:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *demoLbl = (id)view;
if (!demoLbl) {
demoLbl= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
}
demoLbl.text = #"Demo";
demoLbl.textColor = [UIColor redColor];
demoLbl.font = [UIFont systemFontOfSize:14];
return demoLbl;
}

UiPickerView change font color according data

I'm using a pickerView with multiple components related to several fields in a Database (CoreData).
Is it possible to change the fontcolor for a specific component according the presence of data in the DB ?
For example the field in the DB is null the component font color should be RED otherwise black.
Any help will be appreciated !
Dario
==================
Thanks Kenny,
I have to apply to a single UIPicker only. So I', returning the view parametere (without modificatiosn). The result is all the pickers show empty rows.
Thanks for help !
Here you will find the code fragment:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView == tipoPk){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100,30)];
label.textColor = [UIColor redColor];
switch (component) {
case PK_Tipo:
label.text = [tipoArray objectAtIndex:row]];
break;
case PK_Settore:
label.text = [settoreArray objectAtIndex:row]];
break;
default:
break;
}
return label;
}
else {
return view; // <==== return view for non related pickerviews , but no rows shown
}
}
This one also works..
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}
Yes, but you have to use -pickerView:viewForRow:forComponent:reusingView: and supply a custom UILabel.

Changing text size in Pickerview

How can we change the size of the text in a picker view?
Anyone please help.
You can change the font size of UIPickerView using code :
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (id)view;
if (!retval) {
retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];
}
retval.font = [UIFont systemFontOfSize:22];
return retval;
}
For completeness, yakub_moriss code lacks of one line, which was in the post signaled by indragie. Without a line as
retval.text = [pickerViewArray objectAtIndex:row];
the picker will be empty.
This is also a great way to change the alignment of the text in the component too, change the textAlignment of the retval label.
Also, the background for the label should be set to clear:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *retval = (id)view;
if (!retval) {
retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
[pickerView rowSizeForComponent:component].width,
[pickerView rowSizeForComponent:component].height)];
}
retval.opaque = NO;
retval.backgroundColor = [UIColor clearColor];
retval.font = [UIFont systemFontOfSize:28];
if (component == 0) {
retval.textAlignment = UITextAlignmentRight;
retval.text = [NSString stringWithFormat:#"%d ", row];
} else {
retval.textAlignment = UITextAlignmentLeft;
retval.text = [NSString stringWithFormat:#" .%d", row];
}
return retval;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 100;
}
return 88;
}
I'm using a category for this. It relies on this other category I have: http://compileyouidontevenknowyou.blogspot.com/2012/06/adding-properties-to-class-you-dont.html.
Anyway, this one looks like this:
#import <UIKit/UIKit.h>
#interface UIPickerView (CustomViewForRow)
#property (nonatomic, strong) UIFont *customViewForRowFont;
- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
#end
and the .m
#import "UIPickerView+CustomViewForRow.h"
#import "NSObject+AssociatedObjectSupport.h"
#implementation UIPickerView (CustomViewForRow)
static char keyForCustomViewForRowFont;
- (UIFont *)customViewForRowFont {
UIFont *retVal = [self associatedObject:&keyForCustomViewForRowFont];
if (!retVal)
retVal = [UIFont boldSystemFontOfSize:17];
return retVal;
}
- (void)setCustomViewForRowFont:(UIFont *)customViewForRowFont {
[self setAssociatedObject:customViewForRowFont forKey:&keyForCustomViewForRowFont];
}
- (UIView *)customViewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *label;
if (!view) {
view = [[UIView alloc] init];
CGRect frame = CGRectZero;
frame.size = [self rowSizeForComponent:component];
view.frame = frame;
view.backgroundColor = UIColor.clearColor;
label = [[UILabel alloc] init];
label.font = self.customViewForRowFont;
label.backgroundColor = [UIColor clearColor];
frame = view.bounds;
frame.origin.x += 7;
frame.size.width -= 14;
label.frame = frame;
[view addSubview:label];
}
label = [view.subviews objectAtIndex:0];
#try {
NSString *text = [self.delegate pickerView:self titleForRow:row forComponent:component];
if (!text)
text = #"???";
label.text = text;
} #catch (NSException *e) {
label.text = #"???";
}
return view;
}
#end
Try this
https://github.com/kafejo/CCPicker
it's very easy picker :)