Changing UITableViewCell textLabel background color to clear - iphone-sdk-3.0

In my app I have a table view with customViewCells. I subclassed the UITableViewCell class and added an image that will load async and for the text I use cell.textLabel.text = #"someThext".
For the cells the background color is set alternatively to [UIColor darkGrayColor] and [UIColor whiteColor].
When I run the app in the simulator and on the phone the textLabel of the cell has the background white. I want to set it to be clear, because I want the background color of the cell to be full not a strip then white then another strip.
In the init method of my custom cell I added, hoping that the white will turn into red, but it doesn't have any effect:
[self.textLabel setBackgroundColor:[UIColor redColor]];
I tried also:
self.textLabel.backgroundColor = [UIColor redColor];
But this also didn't work... if I add a UILabel as a subview, the background color of the label can be set, but I don't want to do that because when I rotate the phone I want my labels to auto enlarge.
Any ideas why setting the background color of cell.textLabel doesn't work?
Thank you

If you do not want to subclass UITableViewCell you can just add this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
}

The problem is that UIKit sets the cell background color in the -setSelected method. I had the method but didn't have self.textLabel.backgroundColor = [UIColor clearColor]; self.detailTextLabel.backgroundColor = [UIColor clearColor]; in it so I added them and the problem mentioned in the picture was fixed.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.textLabel.backgroundColor = [UIColor clearColor];
self.detailTextLabel.backgroundColor = [UIColor clearColor];
}

Looks like Apple changed something here. Doing exactly this in iOS4 works:
self.textLabel.backgroundColor = [UIColor xxxColor];
self.detailTextLabel.backgroundColor = [UIColor xxxColor];
At least up to the point that the label background is transparent or takes the background color. Still not possible to set an own background color.
Nice that this is fixed, but a bit surprising during tests if you develop with base SDK 4.1 and min. deployment 3.1 for iPhone classic and iPad.

To set a clear background color you can use clearColor:
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
Is this what you mean?

see this post: How do I set UITableViewCellSelectionStyle property to some custom color?
in your case just use a UIView with white background color

I didn't try it but here's a guess... In order to draw faster the opaque property of the label might be set to true by default.
Try
cell.titleLabel.opaque = NO;
cell.titleLabel.backgroundColor = [UIColor clearColor];
If that doesn't work either I would probably just give up at this point and create my own UILabel for the cell.

to set the color of the textlabel you should do this:
t.textColor = [UIColor colorYouWantRGB];

Related

UIInputView background is invisible while keyboard is animating

I have a text field which has a UIInputView input accessory view.
When I tap my text field, and the keyboard comes flying into view and I can see the accessory view's subviews, but it has no visible background.
Once the keyboard animation is complete, the UIInputView's background pops into view.
What can I do to force the UIInputView's background to be visible while the keyboard animation is still going?
Here's my code:
UIInputView *inputView = [[UIInputView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.bounds), 44.0f) inputViewStyle:UIInputViewStyleKeyboard];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectInset(inputView.bounds, 15.0f, 2.0f);
[button setTitle:#"Button" forState:UIControlStateNormal];
[inputView addSubview:button];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.bounds), 44.0f)];
textField.inputAccessoryView = inputView;
[self addSubview:textField];
Not a complete solution, but this impact can be minimized if you set background of your UIInputView manually inside -viewDidLoad: method. Fortunately, no need to remove it after. Right after appearing UIInputView will have a truly keyboard background and a blur effect as well.
UIColor *tmpBackground; //Keyboard have color base on the current device.
if ([UIDevice isPad] || (UIDevice isPod)]) { //These are my helpers, you have to implement it
tmpBackground = [UIColor colorWithRed:207/255.0f green:210/255.0f blue:214/255.0f alpha:1];
} else {
tmpBackground = [UIColor colorWithRed:216/255.0f green:219/255.0f blue:223/255.0f alpha:1];
}
[textField.inputAccessoryView setBackgroundColor:tmpBackground];
P.S. And I know, it is not a perfect solution to define colors in such a stupid way.

UITableViewCell curve line anomaly- left side. What's causing this? Image included

I have this odd semicircle being drawn at the top left of each cell in this Table View. I can't seem to be able to get rid of it. I'm not doing any custom drawing in my cells, although the cells are all subclasses of UITableViewCell.
The curved artifact appears only in the first cell in each section, and is still persistent regardless of the table view background.
This is all the custom code in the cell, the rest is in IB
- (void)layoutSubviews {
// NSLog(#"DataEntryCell layoutSubviews");
UILabel *topLabel = [self parameterLabel];
UILabel *bottomLabel = [self dataLabel];
topLabel.font = [UIFont boldSystemFontOfSize:18.0];
topLabel.textAlignment = UITextAlignmentCenter;
topLabel.textColor = [UIColor darkGrayColor];
topLabel.shadowColor = [UIColor lightGrayColor];
topLabel.shadowOffset = CGSizeMake(0.0, 1.0);
bottomLabel.font = [UIFont systemFontOfSize:16.0];
bottomLabel.textAlignment = UITextAlignmentLeft;
bottomLabel.textColor = [UIColor darkGrayColor];
bottomLabel.numberOfLines = 0;
bottomLabel.lineBreakMode = UILineBreakModeWordWrap;
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.autoresizesSubviews = YES;
}
Any idea what could causing this?
Thanks
I don't see anything in that layoutSubviews that really belongs there. All of those properties on the labels should be set when the labels are created. Ditto for the properties on self. The selectionStyle in particular seems suspicious since it mentions gray and you have a stray gray line. By the time layoutSubviews is called, it's too late to set autoresizesSubviews; autoresizing happens before you receive layoutSubviews.
Try putting that stuff closer to the cell's initialization - in awakeFromNib if you're loading the cell (and its subviews) from a nib, or with the code that creates and adds the subviews.

UITableViewCell custom selectedBackgroundView background is transparent

I have the following code that creates a UIView that I assign to my UITableViewCell's selectedBackgroundView property. Everything works as expected, with the exception of the subview's background, which is transparent.
I use the same code to create a custom view that I assign to backgroundView, and that works fine.
What is causing that subview to be transparent for selectedBackgroundView, and how can I avoid that?
- (UIView*) makeSelectedBackgroundView
{
// dimensions only for relative layout
CGRect containerFrame = CGRectMake(0, 0, 320, 40);
UIView* containerView = [[UIView alloc] initWithFrame:containerFrame];
containerView.autoresizesSubviews = YES;
// dimensions only for relative layout
CGRect subframe = CGRectMake(5, 5, 310, 30);
UIView* subview = [[UIView alloc] initWithFrame:subframe];
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
subview.backgroundColor = [UIColor redColor];
subview.layer.cornerRadius = 5;
subview.layer.borderWidth = 2;
subview.layer.borderColor = [UIColor greenColor].CGColor;
[containerView addSubview:subview];
return containerView;
}
As we can see from name of ivar selectedBackgroundView, this background shown by cell when it was selected.
I've to reload few methods (– setSelected:animated: and – setHighlighted:animated:) of UITableViewCell subclass to reset background color of subviews back to their values. Look's like UIKit do some magic in this template methods (iterating over all UIView subclasses and set their background to clearColor)
This code might be helpful for you:
UIImageView *cellImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,
0,
cell.frame.size.width,
cell.frame.size.height
)];
cellImageView.contentMode = UIViewContentModeScaleAspectFit;
// normal background view
[cellImageView setImage:[UIImage imageNamed:#"*<ImageName>*"]];
[cell addSubview:cellImageView];
[cell sendSubviewToBack:cellImageView];
[cellImageView release], cellImageView = nil;
Here cell is an object of custom UITableViewCell.
Also you can set backgroundColor property.
I would try to set the alpha for both containerView and subView to 1.0
[containerView setAlpha:1.0];
...
[subview setAlpha:1.0];
this should make your controls totally opaque.
You could also create some images for the background and use that images in state of creating 2 views. Let's say you create 2 image (normalBackground.png and selectedBackground.png) and then set this images as cell background. Here is a nice tutorial.
Try setOpaque:YES on your views.
In the end, I ended up subclassing UITableViewCell which contained a custom view object, and that worked.

Change color on checkmark in UITableView

Could someone be so kind to show me how to change the color on the checkmark in UITableView?
I have searched but don't seem to get it to work.
Cheers
Since the iOS SDK has changed since the accepted answer, I thought I'd just update with a new answer.
You can in fact change the color of the checkmark in a UITableViewCell by adjusting the tintColor property of the UITableViewCell.
You can also set an appearance proxy for all UITableViewCells so that ALL instances have a specific tint color unless otherwise specified
[[UITableViewCell appearance] setTintColor:[UIColor redColor]];
Swift:
In Swift change the tintcolor to the color you want to change the color of any Accessory Type
cell.tintColor = .black
Apple doesn't provide a public way to change the color of the checkmark so you'll have to do it with an image.
This is very simple, just set the accesoryView property of the cell to a UIImageView containing a checkmark of the correct color.
It'll look like this:
UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coloredCheckmark.png"]];
cell.accessoryView = checkmark;
[checkmark release];
Enjoy!
If you are looking for a Swift version:
Directly on the cell
For example in tableView(_:,cellForRowAtIndexPath:)
cell.tintColor = UIColor.redColor()
Using the appearance protocol
UITableViewCell.appearance().tintColor = UIColor.redColor()
The following worked for me in iOS 7.
[self.tableView setTintColor:[UIColor someColor]];
This image shows how to do this in storyboards.The Tint color is the checkmark color.
I found that igraczech's answer is mostly correct, but with iOS 6 or later, you can just set the tint color of the entire tableview and default items will inherit down.
[self.tableView setTintColor:[UIColor someColor]];
This worked for me and allowed me to color in the checkmark.
Starting iOS 7 you could set the tint color of your view controller's view so that this tint colow will be propageted to all it's child views. So to set your UITableViewCell's checkmark as purple color (for example), in your viewWillAppear method you need to:
[self.view setTintColor:[UIColor purpleColor]];
The UIAccessoryTypeCheckmark (right side) inherits background color of its tableview.
self.tableView.backgroundColor = [UIColor clearColor];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HNCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HNCustomTableViewCell" forIndexPath:indexPath];
cell.tintColor = [UIColor colorWithRed:0.99 green:0.74 blue:0.10 alpha:1.0];
return cell;
}
It work for me.
UITableViewCell *cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
cell.tintColor=UIColor.whiteColor;
return cell;
#import "UIImage+Color.h"
UIImage *image = [[UIImage imageNamed:#"ic_check_black_24dp.png"] changeColor:CLR_BUY];
UIImageView *checkmark = [[UIImageView alloc] initWithImage:image];
cell.accessoryView = checkmark;
I always used this easy way:
UITableViewCell *cell = ...;
cell.tintColor = [UIColor greenColor];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
You don't have to use your own image, you can simply change it in your view did load with the following code.
[self.tableView setTintColor:[UIColor colorWithRed:250/255.0 green:223/255.0 blue:6/255.0 alpha:1]]
Cheers!
Swift 3.1, iOS 9+
if #available(iOS 9.0, *) {
UIImageView.appearance(whenContainedInInstancesOf: [UITableViewCell.self]).tintColor = UIColor.themeTint //add your color here
} else {
// Fallback on earlier versions
}
The above answers are all great. But if you want to do it globally, just do
UITableViewCell.appearance().tintColor = .green
Nice little trick :)

Objective C: How to make background color of UITableView Consistent

I have been trying to set the background color of my table view but am facing an issue
This is what I am trying to do.
//Set background color of table view (translucent)
self.tableView.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:0.7];
//Set frame for tableview
[self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.picker.frame.size.height)];
After setting the table cells, I saw that there is an inconsistency in the alpha level around the table view cell (see screenshot below)
Is there anyway to make the color / alpha level consistent for the background?
(Note: I am not setting a background image, only color and alpha level)
Thanks!
Zhen Hoe
I recently ran into this problem myself and think I found the solution
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.2 blue:0.5 alpha:0.7];
Give that a shot, might need some massaging for your specific case (looks like you have some picture behind that translucent blue too).
You need to make the background of the UITableView clear as well as set Opaque to FALSE
[self.tableView setBackgroundColor: [UIColor clearColor]];
[self.tableView setOpaque: NO];
All you need to do are 2 things:
E.g. A view has a table view in it.
In the XIB file set the background of the UITableView to clear color.
Set the background of the container view of the table view to the image you want.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"app_background.png"]];
If the ViewController is actually a TableViewController then set:
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"app_background.png"]];
You can set the row background with inside the table view delegate (the view or tableview controller)
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] % 2 == 0) {//248 G:192 B:100
[cell setBackgroundColor: [UIColor redColor]];
} else {
[cell setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"xyz.png"]]];
}
}