Set all UILabels to subclass programmatically

136 Views Asked by At

I have many many many existing UILabels in a project that I want to subclass. Rather than spending days going through them all individually in IB setting them to my UILabel subclass want to set them programmatically

Checked here: How to programmatically set custom class?

Couple of guesses tried: UILabel_setClass(self.contentView, [MyLabel class]); - implicit declaration error

and:

        Class  lbl = [[MyLabel alloc] init];

        for (id view in [self.contentView subviews]) {
            if ([view isKindOfClass:[UILabel class]]) {

                 //....sub it here?

                 }
        }
1

There are 1 best solutions below

1
On

First of all, it's very bad idea to change object class. It's one of kind of smell code. If in the future a new developer will use default UILabel class it can get strange errors. Runtime - is a great and worst feature of objective-C. It can create hidden bugs in bad hands. So use it very carefully and twice think - do you really need it in the project. And better - write your code explicit, use design patters etc.

In your question is not clear why do you need to change the object class. If you need it to change appearance - then use UIAppearance. If you need extend functionality, use Extensions.