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 we want to set some custom fonts. Lets create a category that add functionality to UIFont class. Open your Xcode project, click on File -> New -> File and choose Objective-C file , click Next enter your category name say “CustomFont” choose file type as Category and Class as UIFont then Click “Next” followed by “Create.”

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.

+(UIFont *)productSansRegularFontWithSize:(CGFloat)size{
    
    return [UIFont fontWithName:@"ProductSans-Regular" size:size];
    
}

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:


Categories:
* Create a Category

Table Of Contents
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
73 Categories
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable