Change specific row color uipicker - iphone

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

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;

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

How to change the Size of SectionIndexTitles in UITableView?

Can any one tell me, is it possible to change the default size of sectionIndexTitles in UITableView? If Yes then how?
for(UILabel *aView in [tableView subviews])
{
NSLog(#"frame:%#",aView);
if([[[aView class] description] isEqualToString:#"UITableViewIndex"])
{
aView.font=[UIFont fontWithName:#"Helvetica-Bold" size:18.0];
}
}
You can customise this as per your needs.
You need to implement
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
in your UITableViewDelegate as described here
Here's an example:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)] autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
UILabel * headerLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)];
[headerLabel setBackgroundColor:HEXCOLOR(0x952039FF)];
[headerLabel setTextColor:[UIColor whiteColor]];
[headerLabel setShadowColor:[UIColor grayColor]];
CGRect frame = headerLabel.frame;
frame.origin = CGPointMake(20,frame.origin.y);
headerLabel.frame = frame;
[headerLabel setText:[self sectionTitles:section]];
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
sectionTitles refers to a simple function returning the section titles.

Selected state of UIButton in UITableView

I have UIButton in each cell of UITableView. When I touch it, its state is set to selected. But when I scroll table view so the button isn't visible, the state is set to normal. How can I do it that UIButton remain selected?
Thank you.
Edit: Here is the cellForRowAtIndexPath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row < [messagesArray count]) {
Zprava *msgObj = [messagesArray objectAtIndex:indexPath.row];
int width = 0;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
width = 320;
} else {
width = 480;
}
CGSize boundingSize = CGSizeMake(width, CGFLOAT_MAX);
CGSize requiredSize = [msgObj.mmessage sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
UIButton *background = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, 300, requiredSize.height + 20)];
[background setBackgroundImage:[[UIImage imageNamed:#"balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateNormal];
[background setBackgroundImage:[[UIImage imageNamed:#"selected-balloon.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:15] forState:UIControlStateSelected];
[background addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
background.tag = msgObj.msgID;
[[cell contentView] addSubview:background];
[background release];
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 285, 15)];
[dateLabel setFont:[UIFont systemFontOfSize:12]];
[dateLabel setLineBreakMode:UILineBreakModeWordWrap];
[dateLabel setTextColor:[UIColor lightGrayColor]];
[dateLabel setNumberOfLines:0];
[dateLabel setTextAlignment:UITextAlignmentRight];
[dateLabel setText:msgObj.mdate];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setOpaque:NO];
[[cell contentView] addSubview:dateLabel];
[dateLabel release];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 275, requiredSize.height + 5)];
[messageLabel setFont:[UIFont systemFontOfSize:17]];
[messageLabel setLineBreakMode:UILineBreakModeWordWrap];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setNumberOfLines:0];
[messageLabel setText:msgObj.mmessage];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setOpaque:NO];
[[cell contentView] addSubview:messageLabel];
[messageLabel release];
}
return cell;
}
Save button states somewhere in your model separately from table view and set buttons state in cellForRowAtIndexpath: method again.

UIPickerView Row Color

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