30

I have been getting unreliable results while trying to apply UIAppearance proxy styles to the UILabel class proxy. For example, the following works as I would expect:

[[UILabel appearance] setFont:[UIFont fontWithName:SOME_FONT size:SOME_SIZE]];
[[UILabel appearance] setShadowColor:[UIColor blackColor]];

Setting the textColor doesn't work, however, this:

[[UILabel appearance] setColor:[UIColor greenColor]];

does work. Kind of. It's somewhat unreliable and causes any instance-specific calls to setTextColor: to be ignored.

What is the correct way to apply UIAppearance styles to a UILabel?

Joshua J. McKinnon
  • 1,696
  • 1
  • 18
  • 29

4 Answers4

56

OK, it turns out that you cannot style any UILabel properties using the UIAppearance proxy.

While the UILabel class conforms to the UIAppearanceContainer protocol, a check of UILabel.h shows that none of its properties are marked with UI_APPEARANCE_SELECTOR, the prerequisite for the use of UIAppearance.

Bugger.

Joshua J. McKinnon
  • 1,696
  • 1
  • 18
  • 29
  • 2
    This is still mind boggling to me. UIButton is also completely unstylable with UIAppearance. I filed a radar in March of 2012. One can only hope they remedy this in iOS 7. – rcw3 Jun 02 '13 at 01:21
  • 3
    this is terrible! terrible! – abbood Mar 06 '14 at 11:11
  • 2
    I do not believe this is still true. At least, I was able to style UILabels using the UIAppearance proxy in my app. The problem I encountered was the areas where I DIDN'T want my labels styled. Such as within a UISegmentedControl. I tried using appearanceWhenContainedIn: to fix this, but that was complicated and I ended up taking out the appearance proxy and just styling manually. – mbm29414 Jun 11 '14 at 18:30
  • `[[UILabel appearance] setTextColor:[UIColor greenColor]]` does work for me on iOS 8.3. – Mojo66 Jun 24 '15 at 12:46
  • 1
    Yes, @Mojo66, it does *kind of* work, see my original question - it's not reliable, at least it wasn't. – Joshua J. McKinnon Jun 25 '15 at 00:32
  • 1
    In iOS11 stills not able to configure the color, is this about or there is a reason for this? I cannot believe Apple does not have the man power to address this... – PakitoV Sep 26 '17 at 13:16
  • Setting the color is working for me in iOS 11 - see here: https://www.codeday.top/2017/08/06/32408.html – smörkex Nov 15 '17 at 09:20
1

I have subclassed UILabel

@interface SmallLabel : UILabel

@end

@implementation SmallLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

@end

Then I use appearanceWhenContainedIn:

UIFont *smallFont = [UIFont fontWithName:@"Arial" size:15];
[[SmallLabel appearanceWhenContainedIn:[UIView class], nil] setFont:smallFont];

This works to use the desired font for all SmallLabels in my app. I just need to set the Custom Class to SmallLabel in the XCode Identity Inspector. It does not seem to work with labels create programmatically, only those in NIBs.

After further testing this method does not always work reliably.

David Pettigrew
  • 299
  • 2
  • 10
1

In Swift you do the following to customize the appearance attributes for a UILabel when contained in a UITableViewHeaderFooterView:

UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 18)
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .white

This will apply to the textLabel attribute when you use UITableViewHeaderFooterView in:

 public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerViewId)
    if headerView == nil {
        headerView = UITableViewHeaderFooterView(reuseIdentifier: headerViewId)
    }
    headerView?.textLabel?.text = "My Header".uppercased()
    return headerView
}

This really helps when using the UITableViewStyle.grouped, as those section header views seem to be overridden with a default style even if you customize the UITableViewHeaderFooterView.textLabel in viewForHeaderInSection

Kevin Bloom
  • 71
  • 1
  • 5
0

Following code worked for me using swift 2.2 and for iOS 9.0

    let textFieldInsideSearchBar = self.searchBar?.valueForKey("searchField") as? UITextField
    textFieldInsideSearchBar?.textColor = BCGConstants.Colors.darkBluishPurpleColor()

    let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
    textFieldInsideSearchBarLabel?.textColor = UIColor(red: 220/255, green: 209/255, blue: 231/255, alpha: 1.0)`enter code here`
Mohsin Qureshi
  • 1,203
  • 2
  • 16
  • 26