Georg
Georg

Reputation: 3930

UIAppearance proxy problems with appearanceWhenContainedIn

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?!?

Upvotes: 0

Views: 3017

Answers (3)

Peter Blazejewicz
Peter Blazejewicz

Reputation: 97

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).

Upvotes: -2

Joshua J. McKinnon
Joshua J. McKinnon

Reputation: 1696

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)

Upvotes: 2

dasdom
dasdom

Reputation: 14063

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

Upvotes: 1

Related Questions