Search code examples
iosobjective-cobjective-c-category

Category is implementing a method which will also be implemented by its primary class ?


I have Got this waning while using this code

-(void) drawPlaceholderInRect:(CGRect)rect {
    // [[UIColor ] setFill];

    BOOL currentdevice=[[UIDevice currentDevice].model hasPrefix:@"iPhone"];
    if(currentdevice)
    {
        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:15] }];

    }
    else
    {

        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:20]}];
    }

}

To Stop the warning message i am using this code

@implementation UITextField (placeholder)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

// do your override
-(void) drawPlaceholderInRect:(CGRect)rect {
    // [[UIColor ] setFill];

    BOOL currentdevice=[[UIDevice currentDevice].model hasPrefix:@"iPhone"];
    if(currentdevice)
    {

        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:15] }];

    }
    else
    {

        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:20]}];
    }

}


#pragma clang diagnostic pop 

is there any problem if i use like this ..

or please give me exact way to override the method thanks....


Solution

  • For non UI customization its better to subclass. Assign TextField Class as CustomTextField where ever you require drawPlaceholderInRect as per your implementation

    // CustomTextField.h

        #import <UIKit/UIKit.h>
    
        @interface CustomTextField : UITextField
    
        @end
    

    //CustomTextField.m

    #import "CustomTextField.h"
    
    @implementation CustomTextField
    
    - (void)drawPlaceholderInRect:(CGRect)rect {
    
        BOOL currentdevice=[[UIDevice currentDevice].model hasPrefix:@"iPhone"];
        if(currentdevice){
            NSDictionary *attrDictionary = @{
                                             NSForegroundColorAttributeName: [UIColor redColor],
                                             NSFontAttributeName : [UIFont fontWithName:@"OpenSans" size:15.0]
                                             };
    
            [[self placeholder] drawInRect:rect withAttributes:attrDictionary];
    
        }
        else{
    
            NSDictionary *attrDictionary = @{
                                             NSForegroundColorAttributeName: [UIColor redColor],
                                             NSFontAttributeName : [UIFont fontWithName:@"OpenSans" size:20.0]
                                             };
    
            [[self placeholder] drawInRect:rect withAttributes:attrDictionary];
        }
    
      }
    @end
    

    Set Class as custom class