Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Subclasses in Objective-C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 405
    Comment on it

     

    Hi Reader’s,

     

    In this blog we are going to discuss the concept of subclasses in Objective-C language. Subclasses are basically used to change the methods of an existing class or changes the behavior of properties we will use in a class. With the help of subclasses we can modify or change the inbuilt methods or properties according to our need. Concept of inheritance is used in subclasses as from the name we will get to know the concept that is sub class of a super class so here subclass inherit some properties from superclass and than we will do the modifications according to our need. Here we will have a look on different subclasses of different super classes one by one. Most important thing is this one subclass can be used by multiple objects of the application.


    So first of all we will start with the subclass of UITextField and we will take a new file with super class as UITextField. With the help of this subclass we will provide bottom border to the textfield.

     

    For Example:-

     

     

    TextFieldSubClass.h
    
    
    #import <UIKit/UIKit.h>
    
    @interface TextFieldSubClass : UITextField
    
    @end
    
    TextFieldSubClass.m
    
    #import "TextFieldSubClass.h"
    
    @implementation TextFieldSubClass
    
    
    - (void)drawRect:(CGRect)rect {
        // Drawing code
        CALayer *border = [CALayer layer];
        CGFloat borderWidth = 2;
        border.borderColor = [UIColor redColor].CGColor;
        border.frame = CGRectMake(0, self.bounds.size.height - borderWidth, self.bounds.size.width, self.bounds.size.height);
        border.borderWidth = borderWidth;
        [self.layer addSublayer:border];
        // [border removeFromSuperlayer];
        self.layer.masksToBounds = YES;
    }
    
    
    - (void)layoutSubviews {
        [super layoutSubviews];
    
    }
    
    @end

     

     

     

     


    Now we will create an another subclass  of Textfield to change the placeholder text color in the following way.


     

    SubclassToChangePlaceholderTextColor.h
    
    
    #import <UIKit/UIKit.h>
    
    @interface SubclassToChangePlaceholderTextColor : UITextField
    
    @end
    
    
    
    SubclassToChangePlaceholderTextColor.m
    
    
    #import "SubclassToChangePlaceholderTextColor.h"
    
    @implementation SubclassToChangePlaceholderTextColor
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    - (void)drawPlaceholderInRect:(CGRect)rect {
        // Set color and font size and style of placeholder text
        [[UIColor blueColor] setFill]; //set placeholder text color to red
        [[self placeholder] drawInRect:rect withFont:[UIFont fontWithName:@"verdana" size:14.0]]; //set custom font style and size to placeholder text
    }
    @end

     

     


     

    You can also download the project with the help of given link:-

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: