0

I am trying to give each label in my background view a shadow:

[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowOffset:CGSizeMake(0, -1)];

The problem is that in my background view there are some subviews (a tableview for example) which cells' labels should not get this shadowColor.

I tried this by doing so:

[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
[[UILabel appearanceWhenContainedIn:[MyBackgroundView class], nil] setShadowOffset:CGSizeMake(0, -1)];
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setShadowColor:[UIColor clearColor]];

But the text-shadow still exists in the tableviews' cells.

Can anybody tell me what I am doing wrong?!?

Georg
  • 3,664
  • 3
  • 34
  • 75

3 Answers3

2

You can't use the UIAppearance proxy to customise UILabel at all. See this question. Attempting to do so, in my experience, leads to inconsistent and confusing results.

(I also saw the problem where setting appearanceWhenContainedIn:[somethingElse] on UILabel causes all other [UILabel appearance] calls to be ignored)

Community
  • 1
  • 1
Joshua J. McKinnon
  • 1,696
  • 1
  • 18
  • 29
  • It's pretty straight forward. All UILabels contained here will conform to this appearance proxy when the view appears on screen. UIButtons have bugs with appearance proxies due to the way they set their title label fonts.... but UILabels are about as functional and straight forward as it gets. ... – TheCodingArt Jun 17 '14 at 13:28
1

I would create a sub class of UILabel and set the shadow appearance on that.

dasdom
  • 13,975
  • 2
  • 47
  • 58
-2

I think you have two options:

  1. You could enclose that modified controls in own container and use:

    @implementation ChildViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [[UILabel appearanceWhenContainedIn:self.class, nil] setShadowColor:[UIColor colorWithWhite:0.6 alpha:1]];
        [[UILabel appearanceWhenContainedIn:self.class, nil] setShadowOffset:CGSizeMake(5.0, 5.0)];
    }
    
    @end
    

    Changes will be applied only to UILabel instances hosted within ChildViewController container

  2. Or you could subclass UILabel as suggested to avoid chaining appearance changes within your current container (so other labels in e.g. in cells are not affected).