Ios -[UITextField drawPlaceholderInRect:]未被调用

Ios -[UITextField drawPlaceholderInRect:]未被调用,ios,uitextfield,Ios,Uitextfield,我正在开发一个应用程序,其中应该有一个建议给用户,他可以接受或放弃它。现在让我们假设我只想向用户显示建议 我正在使用drawTextInRect:和drawPlaceholderInRect: drawTextInRect:按预期工作,但DrawPlaceholdinRect:只会被调用两次:第一次是在文本字段首次出现时,然后是在其内部单击时。之后,我猜它会缓存结果,并且不再调用drawPlaceholderinRect:ever 下面是示例代码: #import <UIKit/UIKit

我正在开发一个应用程序,其中应该有一个建议给用户,他可以接受或放弃它。现在让我们假设我只想向用户显示建议

我正在使用drawTextInRect:和drawPlaceholderInRect:

drawTextInRect:按预期工作,但DrawPlaceholdinRect:只会被调用两次:第一次是在文本字段首次出现时,然后是在其内部单击时。之后,我猜它会缓存结果,并且不再调用drawPlaceholderinRect:ever

下面是示例代码:

#import <UIKit/UIKit.h>    
#import "CustomTextField.h"

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet CustomTextField *field1;

@end
复制步骤:

单击文本字段 将field1.1更改为true 单击文本字段并关闭键盘 文本字段中显示的文本应为绿色abcde
#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.field1.suggestionText = @"abcde";
}

- (IBAction)toggleSuggesting:(id)sender {
    self.field1.suggesting = !self.field1.isSuggesting;
    [self.field1 setNeedsDisplay];
}

@end
#import <UIKit/UIKit.h>

@interface CustomTextField : UITextField

@property(assign,nonatomic,getter=isRequired) BOOL required;
@property(assign,nonatomic,getter=isSuggesting) BOOL suggesting;
@property(strong,nonatomic) NSString* suggestionText;

@end
#import "CustomTextField.h"

@implementation CustomTextField

-(void)drawPlaceholderInRect:(CGRect)rect {
    if ( _suggesting && [self.suggestionText length] > 0 ) {
        [self drawSuggestionInRect:rect];
    }
    else {
        [super drawPlaceholderInRect:rect];
    }
}

-(void)drawTextInRect:(CGRect)rect {
    if ( _suggesting && [self.suggestionText length] > 0 ) {
        [self drawSuggestionInRect:rect];
    }
    else {
        [super drawTextInRect:rect];
    }
}

-(void)drawSuggestionInRect:(CGRect)rect {
    [[UIColor greenColor] setFill];
    [self.suggestionText drawInRect:rect withFont:self.font];
}
@end