GCPlaceholderTextView
GCPlaceholderTextView copied to clipboard
Change Request
@gcamp Can you add a condition if character length is 0 then it should show Place Holder with blinking cursor.
UITextField doesn't behave like that so I don't think this is a good idea.
This is exactly how UITextField
behaves — the placeholder text is shown whether or not the field is the first responder, as long as the field has no text in it.
See this video for reference - the placeholder text is displayed until the user begins entering text, and it reappears when the user clears out the UITextField
.
Hmm, did that change? Reopening this.
Thanks for putting this together. Currently, when you focus on the field, the placeholder text disappears. This is not how UITextField
behaves. The placeholder text should be visible as long as there are no characters inputted into the text field.
I modified your class to add a UILabel
as a subview of the GCPlaceholderTextView
. The UILabel
displays the placeholder text which just matches the font of self.font and is always present as long as self.text.length
is 0. Once it's greater than 0, I simply remove the UILabel
from the view. Works quite well, and is far less code to implement.
@chrysb can you make a pull request referring this issue?
It basically modifies everything - here's the code. I'm not sure if the frame for the label is set in a way that will work in every situation. It works for mine, and that's all I have time for at the moment. Hope this helps!
#import <UIKit/UIKit.h>
@interface GCPlaceholderTextView : UITextView
@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, strong) UIColor *placeholderColor;
@end
#import "GCPlaceholderTextView.h"
@interface GCPlaceholderTextView ()
@property (nonatomic, strong) UILabel* placeholderLabel;
@end
@implementation GCPlaceholderTextView
@synthesize placeholder;
@synthesize placeholderColor;
@synthesize placeholderLabel;
#pragma mark -
#pragma mark Initialisation
- (id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChange:) name:UITextViewTextDidChangeNotification object:self];
}
#pragma mark -
#pragma mark Setter/Getters
- (void) setPlaceholder:(NSString *)aPlaceholder {
if (aPlaceholder != placeholder) {
placeholder = aPlaceholder;
}
placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 3.5, 280, 30)];
placeholderLabel.textColor = (placeholderColor == nil) ? [UIColor colorWithRed:179/255.0f green:179/255.0f blue:179/255.0f alpha:1.0f] : placeholderColor;
placeholderLabel.text = self.placeholder;
placeholderLabel.tag = 1;
placeholderLabel.font = self.font;
[self addSubview:placeholderLabel];
}
- (void) didChange: (NSNotification*) notification {
if ([self.text isEqualToString:@""] || self.text == nil) {
[self addSubview:placeholderLabel];
}
else {
[[self viewWithTag:1] removeFromSuperview];
}
}
#pragma mark -
#pragma mark Dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
@gcamp Do you think about integrate this to your project ?