From b1b319053c5b1867d46e0c511882a52c2f5cf786 Mon Sep 17 00:00:00 2001 From: Ben Ahrens Date: Sun, 21 Aug 2011 16:04:26 -0400 Subject: [PATCH] Fixed issue of placeholder not displaying when setting text to nil or empty Fixed issue of placeholder not displaying when setting text to nil or empty --- GCPlaceholderTextView/GCPlaceholderTextView.m | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/GCPlaceholderTextView/GCPlaceholderTextView.m b/GCPlaceholderTextView/GCPlaceholderTextView.m index f1b2e0d..8d1cd2e 100644 --- a/GCPlaceholderTextView/GCPlaceholderTextView.m +++ b/GCPlaceholderTextView/GCPlaceholderTextView.m @@ -60,11 +60,20 @@ - (NSString *) text { return text; } -- (void) setText:(NSString *)text { - super.text = text; +- (void) setText:(NSString *)text { + if ([text isEqualToString:@""] || text == nil) { + super.text = self.placeholder; + } + else { + super.text = text; + } - if ([text isEqualToString:self.placeholder]) self.textColor = [UIColor lightGrayColor]; - else self.textColor = self.realTextColor; + if ([text isEqualToString:self.placeholder]) { + self.textColor = [UIColor lightGrayColor]; + } + else { + self.textColor = self.realTextColor; + } } - (NSString *) realText { @@ -73,14 +82,14 @@ - (NSString *) realText { - (void) beginEditing:(NSNotification*) notification { if ([self.realText isEqualToString:self.placeholder]) { - self.text = nil; + super.text = nil; self.textColor = self.realTextColor; } } - (void) endEditing:(NSNotification*) notification { if ([self.realText isEqualToString:@""] || self.realText == nil) { - self.text = self.placeholder; + super.text = self.placeholder; self.textColor = [UIColor lightGrayColor]; } }