Setting Cell Text Color - iphone

Apparently this is deprecated :
cell.textColor = [UIColor whiteColor];
Does anyone know what the best way to change the color of a Cell Text ?
Edit after comment :
I'm now using this :
cell.textLabel.textColor = [UIColor whiteColor];
but the color is still not changing, see the IB parameters :

cell.textLabel.textColor = [UIColor whiteColor];

Like says Apple on the UITableViewCell page http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
The color of the title text. (Deprecated in iOS 3.0. Instead set the text color attribute of the UILabel objects assigned to the textLabel and detailTextLabel properties.)
You must use the textLabel instead textColor ;-)
Edit : you must add your OWN textLabel.
You must forget cell.text

Related

Table Cell background and text color cannot be set accurately.

I need to set the background of table cells to a specific color.
(#222222 or RGB(32,32,32) respectively)
The background of the table view in IB is set properly. The correct gray appears in the back of the table header and in section headers etc.
But I struggle with the cells.
To customize the cell's apperance I subclass UITableCell and implement the layoutSubviews method.
This works fine:
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor darkGrayColor];
self.contentView.backgroundColor = [UIColor darkGrayColor];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor grayColor];
}
However, grayColor and darkGrayColor simply do not match the colour that I need.
Naturally I tried the colorWithRed:green:blue:alpha method of UIColor.
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.contentView.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
}
That one results in black background and black color of the detailTextLable.
(Of course it is senseless using the same color for a background and for a text label. I am just trying to work out what colorWithRed:green:blue:alpha does and does not.)
With plain style tables I am fine. Those' cells do not have a background color at all. When I just omit setting the backgroundColor and the contentView's background Color propierties then the background of the cells is displayed as defined as the Table's background color in IB.
But with grouped tables the standard background is some light gray which I want to change to some more decent color that matches my client's style guide.
What am I doing wrong? Do I use colorWithRed:green:blue:alpha properly?
Any suggestion is much appreciated.
I would try an other methods of calucaluting the color float:
[UIColor colorWithRed:(32.0f/255.0f) green:(32.0f/255.0f) blue:(32.0f/255.0f) alpha:1.0f];
since 0 is also include you have 0 to 255 values not 1 to 256 values.
If you want the cell to be transparent use [UIColor clearColor]
32/256 = 0 but 32/256.0 = 0.125.
Thanks for putting your finger to the error.
However, this does not exactly answer the question related to the difference between plain and grouped table style.
Just in case somebody finds the question interesting:
The grouped style comes with a background view. I set that to nil.
The gropued style always has a cell background color set to something. Therefore omitting the self.backgroundColor=... statement was not sufficient.
finally
self.backgroundColor = [UIColor clearColor];
did the trick. It was just not remotely connected to the objective-c mistake that I made too.

iphone : How to draw line on Label?

I want to make my label as shown in the image
I know I can get this effect by putting image view on it.
but is there any other method to do ?
How can I put line on label ?
Try this,
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
blabel.text = #"Hellooooooo";
blabel.textAlignment = UITextAlignmentCenter;
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor blackColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [#"Hellooooooo" sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((blabel.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (blabel.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor blackColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
The line above will appear below the text. You just need to change Y for UIView and it'll do wonders :)
put another label with "_" over it
transparent background.
you can create UIView with line's height and width and give background color to it. Put UIView over your UILabel .
For one of my projects I've created an UILabel subclass, which supports multiline text, underline, strikeout, underline/strikeout line offset, different text alignment and different font sizes.
Please see provided link for more info and usage example.
https://github.com/GuntisTreulands/UnderLineLabel
Place a UIImageView with line image on your label so when you run application it will fit.

Whats the font and color of UITableViewCell text?

I want to replicate the UITableViewCellStyleValue1 provided by apple, I just can't figure out the font and size of the text in the cells in the right. Specifically the font and color of numbers below is, 28 1 6843.
Use the default font with the label with the
size = [UIFont boldSystemFontOfSize:16];
and the color of the number you are looking for is
label.textColor = [UIColor colorWithRed:81.0/255.0 green:102.0/255.0 blue:145.0/255.0 alpha:1.0];
The area on the right of a UITableViewCell is called the detailTextLabel. You can just create a UITableViewCell with the style you want and read the UIFont and UIColor values from it.
UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: #"Example"] autorelease];
UIFont* rightFont = cell.detailTextLabel.font;
UIColor* rightColor = cell.detailTextLabel.textColor;
ITableViewCellStyleValue1
A style for a cell with a label on the left side of the cell with left-aligned and black text; on the right side is a label that has smaller blue text and is right-aligned. The Settings application uses cells in this style.

Change UILabel background color

my code is
UILabel *pTeamAlreadybl=[[UILabel alloc]initWithFrame:CGRectMake(110, 275,180,30)];
pTeamAlreadybl.text=#" Team already?";
pTeamAlreadybl.font=[UIFont boldSystemFontOfSize:16];
pTeamAlreadybl.textAlignment=UITextAlignmentLeft;
pTeamAlreadybl.textColor=[UIColor colorWithRed:61.0/255.0 green:61.0/255.0 blue:61.0/255.0 alpha:0.6];
pTeamAlreadybl.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.7];
How can I change label's background color?
You miss
pTeamAlreadybl.backgroundColor=[UIColor anycolor];
write this code
To change the background color of a UILabel, you need to set the backgroundColor property of the UILabel object. You can do so by using the following line of code:
[pTeamAlreadybl setBackgroundColor:[UIColor colorOfYourChoice]];
OR
pTeamAlreadybl.backgroundColor = [UIColor colorOfYourChoice];
pTeamAlreadybl.backgroundColor = [UIColor ...];
Doesn't the above code work?
[pTeamAlreadybl setBackgroundColor:[UIColor YourColor]];
YourColor as mentioned below
blackColor;
darkGrayColor;
lightGrayColor
whiteColor;
grayColor;
redColor;
greenColor;
blueColor;
cyanColor;
yellowColor;
magentaColor;
orangeColor;
purpleColor;
brownColor;
clearColor;
you can use customized color as well like this
[UIColor colorWithRed:61.0/255.0 green:61.0/255.0 blue:61.0/255.0 alpha:0.6]

UITableView cell background color

how to set different background colors for cells in a UITableView
(specifically rainbow color for seven cells)
Set the backgroundColor property:
cell.backgroundColor = [UIColor redColor];
Note that the backgroundColor must be set in the tableView:willDisplayCell:forRowAtIndexPath: method (from UITableViewCell reference):
Note: If you want to change the
background color of a cell (by setting
the background color of a cell via the
backgroundColor property declared by
UIView) you must do it in the
tableView:willDisplayCell:forRowAtIndexPath:
method of the delegate and not in
tableView:cellForRowAtIndexPath: of
the data source. Changes to the
background colors of cells in a
group-style table view has an effect
in iOS 3.0 that is different than
previous versions of the operating
system. It now affects the area inside
the rounded rectangle instead of the
area outside of it.
Use the indexPath parameter to achieve the rainbow effect.
If you want to set cell color based on some state in the actual cell data object, then this is another approach:
If you add this method to your table view delegate:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}
Then in your cellForRowAtIndexPath method you can do:
if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
cell.contentView.backgroundColor = [UIColor blueColor];
}
This saves having to retrieve your data objects again in the willDisplayCell method.
Do not forget to set background color of your tableView to clearColor. Otherwise the clearColor of cell will not be displayed as the background color of tableView will be whiteColor by default. And even when the cell color turns to clearColor whiteColor will be displayed because the tableView background color is still whiteColor. Remenber.
You can set it like so:
cell.contentView.backgroundColor = [UIColor colorWithRed:249.0/255 green:237.0/255 blue:224.0/255 alpha:1.0];
Pretty sure UITableViewCell is a subclass of UIView, so:
cell.backgroundColor = [UIColor blueColor];
For awful rainbow colors, here is an example:
static NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor yellowColor], etc..., nil];
cell.backgroundColor = [colors objectAtIndex:indexPath.row];
Try this
cell.backgroundVIew.backgroundColor = [UIColor orangeColor];
If you've subclassed UITableViewCell, you can reliably set self.backgroundColor in -layoutSubviews. At least in my experience, this works in the odd cases where tableView:willDisplayCell:forRowAtIndexPath: does not.
You may arrive at the conclusion that 'willDisplayCell' is still not working like I did at first. The reason it didn't work for me initially was because I wasn't using colors properly.
Don't forget that when using custom colors you need to divide by 255.0f:
[UIColor colorWithRed:44/255.0f green:50/255.0f blue:65/255.0f alpha:1];
Something like the following will result to a white cell:
//DON'T DO
[UIColor colorWithRed:44 green:50 blue:65 alpha:1];
making it look like it's not doing anything when it really is setting the background to white.