Last active
August 29, 2015 13:56
-
-
Save Januzellij/9127505 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UITextView doesn't have support for placeholder text, which is flat out stupid. Here's a placeholder (note: set the inital textView text to your placeholder text and the initial textColor to your placeholder textColor) | |
Copy and paste this into your UITextViewDelegate (or add onto them if you've already implemented them) | |
(Inspiration from http://stackoverflow.com/questions/1328638/placeholder-in-uitextview) | |
NSString *placeholder = @"placeholder text"; | |
- (void)textViewDidBeginEditing:(UITextView *)textView { | |
if ([textView.text isEqualToString:placeholder]) { | |
// don't ask why this is neccesary, I have no idea. But without it, it doesn't work | |
// it flickers a *little* bit, but it is what it is | |
[self performSelector:@selector(setCursorToBeginning:) withObject:textView afterDelay:CGFLOAT_MIN]; | |
} | |
[textView becomeFirstResponder]; | |
} | |
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { | |
if ([textView.text isEqualToString:placeholder]) { | |
textView.text = @""; | |
textView.textColor = [UIColor blackColor]; // or whatever your regular textColor is | |
} | |
return YES; | |
} | |
- (void)textViewDidChange:(UITextView *)textView { | |
if ([textView.text isEqualToString:@""]) { | |
textView.text = placeholder; | |
[self setCursorToBeginning:textView]; | |
textView.textColor = [UIColor lightGrayColor]; // or whatever your placeholder textColor is | |
} | |
} | |
- (void)setCursorToBeginning:(UITextView *)textView { | |
textView.selectedRange = NSMakeRange(0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment