How to call an original function from an overloaded function in a category?

In Objective-C, I have a category for the class:

@interface UILabel(CustomInit) - (id)initWithCoder:(NSCoder *)coder; @end 

What I am doing is writing a custom init function that does some extra things, and what I would like to do in this custom init function is to call the UILabel initWithCoder database. Is it possible? Why so?

EDIT

Thanks. Okay, so my plans are controversial. Can't just overload initWithCoder. Is there a way to achieve the same functionality (where all UILabels get this added initialization step) without overloading initWithCoder? Or maybe there is a sample code for UILabel initWithCoder that I can just rewrite with the code added?

EDIT

Okay, so I want to say that I'm trying:

Can I embed my own font in an iPhone app?

has an answer in which someone manually adds a custom font to iphone using the private GraphicServices GSFontAddFromFile function. I tried this code and it worked great for manually setting the label font. However, if you try to install the font in Interface Builder, it does not load properly, it just drops to the system font. What I wanted to do was download the font manually and automatically set the font of the label with the selected font to IB. This way, I don’t need to issue every label that I put. I also do not need to write a funny label subclass (which was also suggested in this thread and makes a large amount of custom drawing), which I found pretty grotesque. Now I can still subclass all of my shortcuts, but then there is a case of inline labels in other user interface objects, i.e. UIButtons. I would like the inline labels not to be broken either.

Any suggestions would be great. Thanks.

+4
source share
3 answers

How do you feel about this?

Take the source address of the method for initWithCoder at run time and store it in a static variable. Make a swizzle method on it to replace the class implementation with my initWithCoder. And then in my initWithCoder, I would name the original method stored in a static variable.

You can put it in a category and call this class initialization step at the beginning of the program, making sure that it cannot be called twice, or if it does nothing.

It seems dangerous, but I feel it should work.

+1
source

In the Mac OS X Reference Library :

When a category overrides an inherited method , the method in the category can, as usual, invoke the inherited implementation through a super message. However, if a category overrides a method that already exists in the category class , there is no way to call the initial implementation.

+13
source

The Swizzling method should work as kidsedlox suggested.

Exactly the same issue was discussed by Evan Doll in this class at Stanford University.

https://podcasts.apple.com/us/podcast/iphone-application-programming-spring-2009/id384233222

+1
source

All Articles