How to reflect properties of UILabel to another? - ios

Im trying to customize UITableViewController dynamically. So i have changed many properties of cell.textLabel. Now i want to copy these properties to detailTextLabel and to one label i have created through code. How it can be done?
cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
This is my cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
cell.textLabel.text=[_names objectAtIndex:indexPath.row];
cell.textLabel.tag=indexPath.row;
cell.detailTextLabel.text=[_phones objectAtIndex:indexPath.row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"] ];
[imageView setFrame:CGRectMake(380,10,30,50)];
[cell addSubview:imageView];
//customize the seperator
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1000, 1)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
//here i want to copy the properties
return cell;
}

For Swift3
class MyLabel: UILabel {
override func draw(_ rect: CGRect) {
super.draw(rect)
backgroundColor = UIColor(red: 0, green: 0.188235, blue: 0.313725, alpha: 1)
textColor = UIColor.white
font = UIFont(name: "HelveticaNeue", size: 26)
autoresizingMask = .flexibleRightMargin
}
}
Create a subclass of UILabel in this way.
#import "MyLabel.h"
#implementation MyLabel
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
self.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
self.textColor=[UIColor whiteColor];
self.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
self.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}
#end
And now create an object of this MyLabel and your properties will be set automatically and also just assign this class to your label through storyboard to the label in your cell.
Subclassing is the best way for implementing reusable code.
Or else you can even create an extension of class or even an class method in some class which accepts the UILabel and sets the properties but this all are not the best practices. Another problem with extensions is the you can only use self but not super. This may create problems in future when you have to extend the properties.
I hope I am clear and helpful.

You can use this method to make all the labels of UITabelViewCell to same property
Here just loop through the subViews and check whether the subview is of UILabel, If it is of UILabel then set the property you want.
My Code :
- (void)formatTheLabelForCell:(UITableViewCell *)cell
{
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
UILabel *lbl = (UILabel *)view;
lbl.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
lbl.textColor=[UIColor whiteColor];
lbl.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
lbl.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}
}
}

Instead of configuring cell in cellForRowAtIndexPath, it's better if you use custom cell. Add imageView & separatorLineView in your Storyboard/ Nib itself. This way all cells are generated with these default properties. Also if you need to configure something through code, you can code it in your CustomCell.m file like this:
class CustomCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.textLabel.backgroundColor = UIColor.redColor
//do other configurations
}
Edit: Downloading images from web may be the reason for taking long time loading cells. Try downloading images asynchronously. You can also use this library: SDWebImage
Note: I know you want it in Objective C, above code in Swift is just for illustration.

As for swift, you can do this, it will copy all the attributes you applied to textLabel to detailTextLabel.
cell.detailTextLabel.attributedText = cell.textLabel.attributedText

First of all I don't think that there is a function to copy specific properties of a any UIKit component in iOS SDK. Therefore for you will have to write a custom function for this. Additionally there are some issues with your "cellForRowAtIndexPath" as pointed out by others in comments.
There are different solutions to this.
Solution 1:
Write a function in your view controller which take two labels as parameters and copy your desired values.
-(void)copyPropertiesFrom:(UILabel*)label1 toLabel:(UILabel*)label2{
label2.backgroundColor = label1.backgroundColor;
label2.textColor = label1.textColor;
label2.font = label1.font;
label2.autoresizingMask = label1.autoresizingMask;
}
In cellForRowAtIndexPath where you want to copy do this
[self copyPropertiesFrom:cell.titleLabel toLabel:cell.detailTextLabel];
Solution 2(Recommended): This is best in my little experience because you can reuse it in other view controllers. There might be a better approach than this.
Create a category of UILabel. Check this link How do I create a category in Xcode 6 or higher? and also this https://code.tutsplus.com/tutorials/objective-c-categories--mobile-10648
Your function within category will look like this.
-(void)formatLabelToMyStyle{
self.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
self.textColor = [UIColor whiteColor];
self.font = [UIFont fontWithName:#"HelveticaNeue" size:26];
self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
}
You will include header file of category and call this function in your cellForRowAtIndexPath like this
[cell.titleLabel formatLabelToMyStyle];
[cell.detailTextLabel formatLabelToMyStyle];
[cell.customTextLabel formatLabelToMyStyle];
And as for your cellForRowAtIndexPath, larme mentioned in comments "Don't add subview like that in cells because cells are reused" This will keep adding views to your cell hence causing memory issues, Specially when you have large number of cell which in your case is true.

You could use a Category for UILabel or use a subclass of UILabel that should share the same styling.
A Category for the UILabel could look like:
// UILabel+CustomStyle.h
#import <UIKit/UIKit.h>
#interface UILabel (CustomStyle)
-(void) applyCustomStyle;
#end
.m file:
// UILabel+CustomStyle.m
#import "UILabel+CustomStyle.h"
#implementation UILabel (CustomStyle)
-(void) applyCustomStyle {
self.backgroundColor = [UIColor colorWithRed: 0 green: 0.188235 blue: 0.313725 alpha: 1];
self.textColor = [UIColor whiteColor];
self.font = [UIFont fontWithName: #"HelveticaNeue" size: 26];
self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
}
#end
Then you can apply the same styling by simply calling:
#import "UILabel+CustomStyle.h"
[label applyCustomStyle];

If you want to use same label configuration to many places in project. Just subclass as #NikhilManapure said.
OR
If you want to apply same properties to TableViewCell textLabel and detailTextLabel. You should subclass TableViewCell and override Label properties in drawrect method.
Objective-C
#import <UIKit/UIKit.h>
#interface PropertiesCell : UITableViewCell
#end
#import "PropertiesCell.h"
#implementation PropertiesCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[self cellLabelConfigure:self.textLabel];
[self cellLabelConfigure:self.detailTextLabel];
}
- (void)cellLabelConfigure:(UILabel*) contentLabel {
contentLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
contentLabel.textColor=[UIColor whiteColor];
contentLabel.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
contentLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}
#end
Swift
class PropertiesCell: UITableViewCell {
override func draw(_ rect: CGRect) {
super.draw(rect)
cellLabelsConfigure(contentLabel: self.textLabel)
cellLabelsConfigure(contentLabel: self.detailTextLabel)
}
func cellLabelsConfigure(contentLabel: UILabel?) {
contentLabel?.backgroundColor = UIColor(red: 0.0, green: 0.188, blue: 0.313, alpha: 1.0)
contentLabel?.textColor = UIColor.white
contentLabel?.font = UIFont(name: "HelveticaNeue", size: 26.0)
contentLabel?.autoresizingMask = UIViewAutoresizing.flexibleRightMargin
}
}
In storyboard change cell class name to PropertiesCell

Create an extension class and use this copy method to pass all the properties you want to the new label.
#implementation UILabel (Copy)
- (UILabel *)copyProperties {
UILabel *label = [UILabel new];
[self copyPropertiesWithLabel:label];
return label;
}
- (void)copyPropertiesWithLabel:(UILabel *)label {
label.backgroundColor = self.backgroundColor;
label.textColor = self.textColor;
label.font = self.font;
label.autoresizingMask = self.autoresizingMask;
// Add more properties
}
#end
Usage:
// cell.textLabel has now all the properties
[theLabelToBeCopied copyPropertiesWithLabel:cell.textLabel];

Swift3 version:
extension UILabel {
func copyProperties() -> UILabel {
var label = UILabel()
self.copyProperties(with: label)
return label
}
func copyProperties(with label: UILabel) {
label.backgroundColor = self.backgroundColor
label.textColor = self.textColor
label.font = self.font
label.autoresizingMask = self.autoresizingMask
// Add more properties
}
}
Usage:
theLabelToBeCopied.copyProperties(with: cell.textLabel)

you can do this for swift.it will copy for all attributes (textLabel to detailTextLabel).i think #Nikhil Manapure given exact answer.
cell.detailTextLabel.attributedText = cell.textLabel.attributedText

Related

Custom UIView in Custom UITableViewCell designed in Storyboard

I am trying to implement some drawing in custom UITableViewCells. To do that, I have added custom UIView subclass to my cell and implemented the drawing in drawRect method of the custom UIView subclass.
Here's my approach:
I have a UITableView with custom UITableViewCells.
I have subclassed UITableViewCell but the design is implemented directly in my storyboard. The contents of the cell are standard elements (UILabels & UIImageViews) and one custom uiview subclass.
I have set up everything in storyboard using constraints & autolayout.
The custom uiview subclass contains drawing code inside the drawRect method.
The drawing will have to be customized based on some parameters for each UITableViewCell.
Question is, what is the proper way to customize the UIView subclass.
At the moment all I get is a black cube, and I cannot see any drawing... The background color doesn't change and the text does not appear (draw).
EDIT:
In cellForRowAtIndexPath I create the custom UIView using its initWithText: andColor: method:
- (instancetype)initWithText:(NSString *)text andColor:(UIColor *)color
{
NSParameterAssert(text);
self = [super init];
if (self)
{
self.text = text;
[self setBackgroundColor:color];
}
return self;
}
-(void)setBackgroundColor:(UIColor *)backgroundColor
{
self.circleColor = backgroundColor;
[super setBackgroundColor:[UIColor clearColor]];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self.circleColor setFill];
CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMidY(rect),
CGRectGetWidth(rect)/2, 0, 2*M_PI, YES);
CGContextFillPath(context);
}
- (void)drawSubtractedText:(NSString *)text inRect:(CGRect)rect
inContext:(CGContextRef)context
{
CGContextSaveGState(context);
CGContextSetBlendMode(context, kCGBlendModeDestinationOut);
CGFloat pointSize = 8;
UIFont *font = [UIFont boldSystemFontOfSize:pointSize];
CGContextTranslateCTM(context, 0,
(CGRectGetMidY(rect) - (font.lineHeight/2)));
CGRect frame = CGRectMake(0, 0, CGRectGetWidth(rect), font.lineHeight);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.font = font;
label.text = text;
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor blueColor];
[label.layer drawInContext:context];
CGContextRestoreGState(context);
}
You would have given tags in your storyboard for the Label and Image. Use those tags and get the reference of subviews as below
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//some code
UILabel* cellDate = (UILabel *)[cellRef viewWithTag:1]
UIImageView* imageView = (UIImageView *)[cellRef viewWithTag:2]
//some code
}
What you are doing is wrong. You already added UIView reference in story board UITableviewCell. And again you are creating a object for the same custom UIView class in cellForRowIndexpath method. What you can do is, remove the UIView from storyboard and in cellforrowindexpath method like shown below.
CustomUIClass * c = [[CustomUIClass alloc] init];
c.frame = CGRectMake(X, Y, Width, height);
[cell.contentView addSubview:c];
Here add your own frame values to prepare CGRect.

Change UILabel textColor when UITableViewCell is highlighted?

I have a custom UITableViewCell, with an UILabel and an UIImageView. I want to change the background color and the text color when the cell is highlighted. In my CustomCell's setHighlighted method, I have the following piece of code:
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if(self) {
if(highlighted) {
self.title.textColor = [UIColor whiteColor];
} else {
self.title.textColor = [UIColor blackColor];
}
//highlight background
UIView *bgColorView = [[UIView alloc] initWithFrame:self.frame];
bgColorView.backgroundColor = [UIColor blackColor];
[self setSelectedBackgroundView:bgColorView];
}
}
I already tried to put the code for textColor change in the tableView's didSelectRowAtIndexPath, but it's not the effect that I want - I want the text color to change when the user touches down on the cell - not at touch up.
Any suggestions?
You should use the attribute highlightedTextColor. Set the color for the cell inside tableView:cellForRowAtIndexPath and it should look like this:
cell.textLabel.highlightedTextColor = [UIColor blueColor];
try this
[cell.textLabel setHighlightedTextColor:[UIColor yellowColor]]
If you have custom label then use following code
lblPrd.highlightedTextColor = [UIColor whiteColor];
override method
(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
//TODO set view highlighted color
}
in custom tableviewcell
In case you have a Storyboard or XIB, you can simply set this at Attributes Inspector > Label > Highlighted.

Is there a way to change the font color of all instances of UITableview's header

The new iOS 7 has changed the default font color of the section headers in tableview. The problem is that my background image makes the text hard to read. I know I could change my background but I would like to change the color of all the textviews. I have changed the uinavigationbar color in my apps delegate before. I would like to use something like that for tableview if possible. I have read this method:
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}else{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(20, 8, 320, 16);
label.textColor = [UIColor whiteColor];
label.text = sectionTitle;
UIView *view = [[UIView alloc] init];
[view addSubview:label];
return view;
}
My problem with this is that I would have to implement it on a per tableviewcontroller basis. Also when using this I'm not sure how to prevent the text from going off the page and being unreadable.
Any suggestions would be great. Thanks in advance.
EDIT: I have decided to add a little clarification just for show using some suggested code here remains my problem with this solution.
EDIT: To accompany answer. I found that this is also needed to create the space for multiple lines for header.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == ?) {
return 60;
}
else{
return 44;
}
}
You must use following method to change your headerview :
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,yourWidth,YourHeight)] ;
headerView.backgroundColor = [UIColor colorWithRed:0.5058f green:0.6118f blue:0.8078f alpha:1.0f];
tableView.sectionHeaderHeight = headerView.frame.size.height;
tableView.tableHeaderView.clipsToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 13,320, 22)] ;
label.text = [self tableView:tableView titleForHeaderInSection:section];
label.font = [UIFont boldSystemFontOfSize:16.0];
label.shadowOffset = CGSizeMake(0, 1);
label.shadowColor = [UIColor grayColor];
label.backgroundColor = [UIColor clearColor];
// Chnage your title color here
label.textColor = [UIColor whiteColor];
[label sizeToFit];
label.numberOfLines = 2 ;
[headerView addSubview:label];
return headerView;
}

How to center the section headers of a UITableView

I want to center the header on the tableview, however I have 2 problems. First I don't have the correct height and second the UILabel doesn't look like the default header - font/font size/color etc... Is there a better way to center it, and/or is there a way to make it look like the default header.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
//section text as a label
UILabel *lbl = [[UILabel alloc] init];
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = #"Header";
[lbl setBackgroundColor:[UIColor clearColor]];
return lbl;
}
this should solve your issue. Tested in xcode 6 / ios8
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextAlignment:NSTextAlignmentCenter];
return [sectionTitles objectAtIndex:section];
}
You must also implement tableView:heightForHeaderInSection to adjust your header height :
In the UITableViewDelegate Protocol Reference doc you find :
tableView:viewForHeaderInSection:
Discussion
The returned object, for example, can
be a UILabel or UIImageView object.
The table view automatically adjusts
the height of the section header to
accommodate the returned view object.
This method only works correctly when
tableView:heightForHeaderInSection: is
also implemented.
For centering the Header label you must specify the label frame before setting its aligment to center.
For getting the standart font, use SystemFontOfSize:
Also beware you are creating a memory leak you are supposed to return an autoreleased view
Try something like this :
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:12];
Hope this helps,
Vincent
Volker's answer in Swift:
If you target iOS6 and later you don't have to provide your own header view if you just want to center the header title.
Just implement
- tableView:willDisplayHeaderView:forSection:
and set the textAligment property of the label:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textAlignment = .Center
}
}
If you target iOS6 and later you don't have to provide your own header view if you just want to center the header title.
Just implement
- tableView:willDisplayHeaderView:forSection:
and set the textAligment property of the label:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
tableViewHeaderFooterView.textLabel.textAlignment = NSTextAlignmentCenter;
}
}
To make this method being called you should first implement
titleForHeaderInSection
then tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) method will be called
Swift Solution:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textAlignment = Localize().isRTL ? .Right : .Left
headerView.textLabel?.text = partsDataSource[section]
headerView.textLabel?.textColor = UIColor ( red: 0.0902, green: 0.2745, blue: 0.2745, alpha: 1.0 )
headerView.textLabel?.font = UIFont(name: UIDecorator.sharedInstance.PRIMARY_FONT, size: 14.0)
headerView.contentView.backgroundColor = UIDecorator.sharedInstance.currentTheme.lightShadeColor
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return " "
}
Here's my version, you'll need to take a screenshot of the normal header background, and save a 1px wide slice of it as 'my_head_bg.png' and add it to the project. This way, it'll look just exactly normal:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
lbl.text = #"My Centered Header";
lbl.textColor = [UIColor whiteColor];
lbl.shadowColor = [UIColor grayColor];
lbl.shadowOffset = CGSizeMake(0,1);
lbl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"my_head_bg"]];
lbl.alpha = 0.9;
return lbl;
}
Try this
UILabel *tableHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[tableHeader setText:#"This is header"];
[tableHeader setTextAlignment:UITextAlignmentCenter];
UITableView *newTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 280, 200) style:UITableViewStylePlain];
[newTable setBackgroundColor:[UIColor clearColor]];
[newTable setTableHeaderView:tableHeader];
self.view = newTable;
In Xamarin:
// Center Header
public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
if (headerView.GetType() == typeof(UITableViewHeaderFooterView))
{
UITableViewHeaderFooterView tableViewHeaderFooterView = (UITableViewHeaderFooterView)headerView;
tableViewHeaderFooterView.TextLabel.TextAlignment = UITextAlignment.Center;
}
}
To change height, implement this method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return 15;
}
I am not really sure about the default header thing - that should be way easier than it is. Maybe someone has a nice background image you can use with a white font. But for the centering thing, try this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,15)];
label.textAlignment = UITextAlignmentCenter
label.text = #"Text Should Be Centered";

Changing the background color of a UILabel within a UITableViewCell

A UITableViewCell comes "pre-built" with a UILabel as its one and only subview after you've init'ed it. I'd really like to change the background color of said label, but no matter what I do the color does not change. The code in question:
UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
Your code snippet works fine for me, but it must be done after the cell has been added to the table and shown, I believe. If called from the initWithFrame:reuseIdentifier:, you'll get an exception, as the UILabel subview has not yet been created.
Probably the best solution is to add your own UILabel, configured to your standards, rather than relying on this (very rickety) path to the built-in one.
This doesn't work because the UITableViewCell sets its label backgroundColors in the layoutSubviews method.
If you want to change the color of the built-in textLabel or detailTextLabel, subclass the UITableViewCell and override layoutSubviews. Call the super implementation, THEN change the backgroundColor property to what you want.
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.backgroundColor = [UIColor redColor];
}
Add your own label to the contentView when you are allocating the cell, rather than relying on the extraction of the built in one. Then you can control all values:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
[cell.contentView addSubview:label];
for (UIView *views in views.subviews)
{
UILabel* temp = (UILabel*)[views.subviews objectAtIndex:0];
temp.textColor = [UIColor whiteColor];
temp.shadowColor = [UIColor blackColor];
temp.shadowOffset = CGSizeMake(0.0f, -1.0f);
}

Resources