Skip to content

Commit

Permalink
added a placeholderColor attribute to set a custom color for the plac…
Browse files Browse the repository at this point in the history
…eholder text. It still defaults to [UIColor lightGreyColor]
  • Loading branch information
David Grandinetti committed Jan 30, 2013
1 parent 1724a2d commit 2c4a1b7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @implementation GCPlaceholderTextViewDemoViewController
- (void)viewDidLoad {
[super viewDidLoad];

self.textView.placeholderColor = [UIColor redColor];
self.textView.placeholder = NSLocalizedString(@"This is a placeholder",);
}

Expand Down
1 change: 1 addition & 0 deletions GCPlaceholderTextView/GCPlaceholderTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
@interface GCPlaceholderTextView : UITextView

@property(nonatomic, retain) NSString *placeholder;
@property (nonatomic, retain) UIColor *placeholderColor;

@end
13 changes: 9 additions & 4 deletions GCPlaceholderTextView/GCPlaceholderTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ @implementation GCPlaceholderTextView

@synthesize realTextColor;
@synthesize placeholder;
@synthesize placeholderColor;

#pragma mark -
#pragma mark Initialisation
Expand All @@ -38,6 +39,7 @@ - (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextViewTextDidEndEditingNotification object:self];

self.realTextColor = [UIColor blackColor];
self.placeholderColor = [UIColor lightGrayColor];
}

#pragma mark -
Expand Down Expand Up @@ -69,7 +71,7 @@ - (void) setText:(NSString *)text {
}

if ([text isEqualToString:self.placeholder]) {
self.textColor = [UIColor lightGrayColor];
self.textColor = self.placeholderColor;
}
else {
self.textColor = self.realTextColor;
Expand All @@ -90,14 +92,17 @@ - (void) beginEditing:(NSNotification*) notification {
- (void) endEditing:(NSNotification*) notification {
if ([self.realText isEqualToString:@""] || self.realText == nil) {
super.text = self.placeholder;
self.textColor = [UIColor lightGrayColor];
self.textColor = self.placeholderColor;
}
}

- (void) setTextColor:(UIColor *)textColor {
if ([self.realText isEqualToString:self.placeholder]) {
if ([textColor isEqual:[UIColor lightGrayColor]]) [super setTextColor:textColor];
else self.realTextColor = textColor;
if ([textColor isEqual:self.placeholderColor]){
[super setTextColor:textColor];
} else {
self.realTextColor = textColor;
}
}
else {
self.realTextColor = textColor;
Expand Down

0 comments on commit 2c4a1b7

Please sign in to comment.