3

I am setting the text color of all the labels in my app using UIAppearance. Yet the text color does not change.

Here is a sample of how i create the label

//show the loading message
MessageLabel *messageLabel = [[MessageLabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = @"\n\nLoading ...\n\n";
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.tableview.backgroundView = messageLabel;

Here is how i set the text color

[[MessageLabel appearance] setTextColor:[UIColor blackColor]];

One note is that all these MessageLabel are BackgroundViews of UITableView

s.d.
  • 363
  • 3
  • 14
  • where r u doing the setTExtColor ? – Teja Nandamuri Dec 25 '15 at 22:39
  • i have a button `Change Text Color`. I do it there. Its just this label that does not update. Other button and labels that i am doing the same update correctly. The only difference is that the other labels and such i have created in IB while this is the only one being created in code – s.d. Dec 25 '15 at 23:31

4 Answers4

2

As arrteme mentioned, UILabel's textColor doesn't work with UIAppearance. I got around this by adding a UIAppearance compatible property that just wraps textColor. In Swift this would look something like

class MessageLabel: UILabel {

    @objc dynamic var configurableTextColor: UIColor {
        get {
            return textColor
        }
        set {
            textColor = newValue
        }
    }

    ...
}

and then to configure the appearance:

MessageLabel.appearance().configurableTextColor = .black
  • This is the only solution I've found that worked for me. I made it an extension instead of a subclass, though, and that still works. `extension UILabel { @objc dynamic var configurableTextColor: UIColor { get { return textColor } set { textColor = newValue } } }` – SwampThingTom May 24 '20 at 20:47
1

From the Documentation:

iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • 2
    i am certainly doing that. Other components are updating correctly with values i set via `UIAppearance` only this has issues – s.d. Dec 25 '15 at 23:29
0

Referring to this, UIAppearance kind of doesn't really seem work with UILabel...

Since you're subclassing from UILabel, maybe it would make sense to set textcolor property initWithFrame: method on in your MessageLabel class?

Or another option, since you say these MessageLabel instances are used for UITableViewCell's background, maybe it would make sense to leave label's background clear and change background of cell itself, for example in tableView:willDisplayCell:forRowAtIndexPath: method in tableView's delegate?

Community
  • 1
  • 1
arrteme
  • 393
  • 1
  • 3
  • 14
0

If you instantiated a UILabel in code, you must manually set textColor property after you set the appearance.

messageLabel.textColor = [[UILabel appearance] textColor];
David Liu
  • 952
  • 7
  • 15