UILabel shadow from custom cell selected color

前端 未结 4 1943
后悔当初
后悔当初 2020-12-29 12:48

I\'m loading a custom nib file to customize the cells of a UITableView. The custom nib has a UILabel that is referenced from the main view by tag. I would like to know if it

相关标签:
4条回答
  • 2020-12-29 13:29

    I prefer to make the shadow color change inside the TableCell code to not pollute the delegate. You can override this method to handle it:

    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animate
    {
        UIColor * newShadow = highlighted ? [UIColor clearColor] : [UIColor whiteColor];
    
        nameLabel.shadowColor = newShadow;
    
        [super setHighlighted:highlighted animated:animate];
    }
    
    0 讨论(0)
  • 2020-12-29 13:40

    The simple answer, at least for the example shown above, is to not display the shadow in the first place. Since you can't see the white-on-white anyway, set the shadowColor to -clearColor.

    If you actually need a shadow though, overriding the -setHighlighted method is the best solution. It keeps the code with the cell, which I think is better than trying to handle it from the table view.

    0 讨论(0)
  • 2020-12-29 13:41

    You could change the label's shadow color in -tableView:willSelectRowAtIndexPath: in the delegate. For instance:

    -(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.shadowColor = [UIColor greenColor];
        return indexPath;
    }
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.shadowColor = [UIColor redColor];
    }
    
    0 讨论(0)
  • 2020-12-29 13:45

    I had the same issue and none of the above solutions quite worked for me - I didn't want to subclass UITableViewCell and also had some tricky selected/highlighted state changes done programmatically, which did not play well with the solutions above.

    MySolution:

    What I did in the end is to use a second UILabel underneath the primary UILabel to act as a shadow. For that 'shadow' UILabel you can set the 'Highlighted Color' to 'Clear Color'.

    Obviously you have to update the shadow label each time you update the primary label. Not a big price to pay in many cases.

    Hope that helps!

    0 讨论(0)
提交回复
热议问题