Today we will learn how to create categories in iOS using Objective c.

Steps:

  1.  Create a new Xcode project (Lets say CategoriesInios)
  2. Now go to StoryBoard and take one Label and a TextField.

Screen Shot 2016-09-22 at 12.20.32 AM.png

 

  1. Make Proper IBOutlet connection.

@property (weak, nonatomic) IBOutlet UITextField *sampleTextField1;

4. Now Go to File- New – File and select Objective C File.

Screen Shot 2016-09-22 at 12.58.09 AM.png5. Click on Next and select following option.

Screen Shot 2016-09-22 at 12.22.50 AM.png6. Click on Next and the File will be created . Now go to UITextField+myTextField.m and write the following Method.

#import “UITextField+myTextField.h”

@implementation UITextField (myTextField)

+ (id)UITextFieldWithPlaceHoldertext:(NSString *)placeHolderText textFieldBackGroundColor:(UIColor *)onTintColor{

    [[UITextField appearance] setPlaceholder:placeHolderText];

    [[UITextField appearance] setBackgroundColor:onTintColor];

    return self;

}

@end

7.  Also declare the method in .h file Also.

#import <UIKit/UIKit.h>

@interface UITextField (myTextField)

+ (id)UITextFieldWithPlaceHoldertext:(NSString *)placeHolderText textFieldBackGroundColor:(UIColor *)onTintColor;

@end

8. Now go to ViewController.m and write the following code in Your ViewDidLoad.

#import “ViewController.h”

#import “UITextField+myTextField.h”

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *sampleTextField1;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.sampleTextField1 = [UITextField UITextFieldWithPlaceHoldertext:@”TextField1″ textFieldBackGroundColor:[UIColor lightGrayColor]];

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

9. Now run the code and see the desired output.

Screen Shot 2016-09-22 at 1.05.24 AM.png

Please do share your reviews and commnets.

You can download the whole source code from here.

Enjoy Coding 🙂

Advertisements