Category 正在实现一种方法,该方法也将由其主要 class 实现?
Category is implementing a method which will also be implemented by its primary class ?
我在使用此代码时遇到了这种情况
-(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]}];
}
}
要停止警告消息,我正在使用此代码
@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
这样使用有什么问题吗..
或者请给我确切的方法来覆盖该方法谢谢....
对于非 UI 自定义,最好将其子类化。 将 TextField Class 指定为 CustomTextField,根据您的实现需要 drawPlaceholderInRect
// 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
我在使用此代码时遇到了这种情况
-(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]}];
}
}
要停止警告消息,我正在使用此代码
@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
这样使用有什么问题吗..
或者请给我确切的方法来覆盖该方法谢谢....
对于非 UI 自定义,最好将其子类化。 将 TextField Class 指定为 CustomTextField,根据您的实现需要 drawPlaceholderInRect
// 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