1

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....

Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47

1 Answers1

0

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

Muhammad Adnan
  • 2,668
  • 15
  • 27