Create a Category

suggest change

Categories provide the ability to add some extra functionality to an object without subclassing or changing the actual object.

For example, to add a custom font helper, create a category on UIFont. In Xcode, choose File -> New -> File, choose Objective-C File, and click Next. Name the category CustomFonts, set the file type to Category and the class to UIFont, then click Next and Create. The files are UIFont+CustomFonts.h and UIFont+CustomFonts.m.

Declare the Category Method :-

Click “UIFont+CustomFonts.h” to view the new category’s header file. Add the following code to the interface to declare the method.

@interface UIFont (CustomFonts)

+(UIFont *)productSansRegularFontWithSize:(CGFloat)size;

@end

Now Implement the Category Method:-

Click “UIFont+CustomFonts.m” to view the category’s implementation file. Add the following code to create a method that will set ProductSansRegular Font.

@implementation UIFont (CustomFonts)

+ (UIFont *)productSansRegularFontWithSize:(CGFloat)size {
    // nil if that font is not installed in the app.
    return [UIFont fontWithName:@"ProductSans-Regular" size:size];
}

@end

Import your category

#import "UIFont+CustomFonts.h"

Now set the Label font

[self.label setFont:[UIFont productSansRegularFontWithSize:16.0]];

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents