Change UILabel textColor when UITableViewCell is highlighted? - ios

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.

Related

Add transparent overlay as subview of `uiimageview`

I have a UITable with custom cells, each of which has a uiimage. When I highlight the cell, the image is not getting highlighted.
To highlight the cell's image, when I select a cell in my table, didHighlightRowAtIndexPath is called. In that method, I change the background color of the cell. I also call a method, indicatedImageViewPressed, which I got from this post, to add an overlay to the image of the cell.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
// Add your Colour.
SpeciesCell* speciesCell = (SpeciesCell*)[tableView cellForRowAtIndexPath:indexPath];
speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
[self indicatedImageViewPressed:speciesCell.imageView on:YES];
}
- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on {
UIView *overlay = [imageView viewWithTag:1]; // See if already created
if (overlay == nil) {
overlay = [[UIView alloc] initWithFrame:imageView.frame];
overlay.tag = 1; // Mark view to find it next time
overlay.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000]; // Your color overlay goes here
[imageView addSubview:overlay];
[imageView bringSubviewToFront:overlay];
}
overlay.hidden = !on;
}
However, the cell's image does not change. No overlay is added. I tried bringSubviewToFront to make sure the overlay wasn't obscured by some other view, but it still doesn't work.
UPDATE: I am able to add a border width/color to speciesCell.speciesImage
speciesCell.contentView.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
speciesCell.speciesImage.backgroundColor = [UIColor colorWithWhite:0.17 alpha:1.000];
replace this with below code:
SpeciesCell.selectionStyle=UITableViewCellSelectionStyleGray;

background for uitableview cell

MyTableCell *cell=[[MyTableCell alloc]init];
cell.backgroundColor = [UIColor clearColor];
The above lines of code are still showing the white color of the cell's background and hiding the background image of the tableview.
Go to you storyboard and set the color of your uitableview cell to clear color from there.And also set uitabelview color also to clear color.
Or in you viewdidload set tableview background color to clear color and in cellforrow at indexparth delagate set
cell.contentView.backGroundColor = [UIColor ClearColor]
try this
- (void)tableView:(UITableView *)tableView willDisplayCell(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
set the contentview background color also
cell.contentView.backGroundColor = [UIColor ClearColor];
cell.backGroundView.backGroundColor = [UIColor ClearColor];
cell.selectedBackGroundView.backGroundColor = [UIColor ClearColor];
Also check if you cleared the tableView.backGroundColor also
Thank you every one. I have solved the problem. I changed the background attribute in the attribute inspector in the storyboard for my cell object from default to clear color and it worked.
You can do it this way
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView.backgroundColor = [UIColor clearColor];

Select row and change button color

In My tableview, cell is custom cell and I have button and label inside it.
lable and button background color is Green.
When I click on Particular row then Button color change and label background color is as it is. Here I used following code to set label background color via layer,
cell.lblCountMsg.backgroundColor = [UIColor clearColor];
cell.lblCountMsg.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0].CGColor;
but, same code is not working for Button. i tried with layer also
cell.btnTeting.backgroundColor = [UIColor clearColor];
cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
but not set green color and also changed the color of selection color.
See, My Screen Shot (Breaking new) is Green.
Please help me to find solution.
Thanks in Advance.
You should implement didSelectRowAtIndexPath something like,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Here your custom cell's class instead of UITableViewCell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.btn.backgroundColor = [UIColor redColor];
}
If you made a UITableViewCell subclass, try overriding the
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
method. In this method you can change your views in the cell when the cell is highlighted.
So for example:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
self.btnTeting.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0];
}
UPDATED
#interface SuggestedGroupCell ()
#property (strong, nonatomic) IBOutlet UIButton *btnSubScribe;
#end
#implementation SuggestedGroupCell
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
self.btnSubScribe.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0];
}
#end
If you want to change background color of button,remove this line and then try:
cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
But if you want to change button title color you can write code like this:
[cell.btnMorePupils setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
Here UIControlState you can take what ever like UIControlStateNormal, UIControlStateSelected etc as per your requirement.

How to get rid of the table cell's selectionStyle but still recieve setSelected/setHighlighted calls

I've tried the method presented here, and everything works except that I cannot make the selectedBackgroundView transparent, it stays white. In my custom UITableViewCell class I have:
-(void)awakeFromNib {
UIView *bkgView = [[UIView alloc]initWithFrame:self.frame];
bkgView.alpha = 0.5;
bkgView.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = bkgView;
}
I've also called [self setNeedsDisplay]; in the setSelected method.
What I want is to remove the blue highlight (from UITableViewCellSelectionStyleBlue
, I don't want to set this to UITableViewCellSelectionNone as it will disable the setHighlight/Selected calls) and still have the highlight/select methods called. I'm so very nearly there, it's just that the selectedBackgorundView remains white! If I set it to redColor or blueColor, it will appear as red or blue, but when I put clearColor, it shows as white. I thought about setting the background with colorWithPatternImage using a transparent PNG image, but I'd like to avoid this.
I prefer to try and figure this out as it's a lot cleaner to rely on these two method calls, and I also want to retain the use of the disclosure indicator which becomes white when selected, which will otherwise stay dark if I use UITableViewCellSelectionNone.
I'd recommend using tableView:willSelectRowAtIndexPath: and returning nil.
That way you still get notified when a Cell is going to be selected. However, it won't actually get selected, and from there you can manage selection yourself without having to deal with the control's default styling.
you use the selectedBackgroundView to override the blue
in viewForRow: method:
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = v;
Set the background view and the selectedBackgroundView:
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor whiteColor];
self.backgroundView = view;
view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView = view;
Then override - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
self.backgroundView.hidden = highlighted;
}

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