GCPlaceholderTextView icon indicating copy to clipboard operation
GCPlaceholderTextView copied to clipboard

Change Request

Open zeeshankhan opened this issue 12 years ago • 8 comments

@gcamp Can you add a condition if character length is 0 then it should show Place Holder with blinking cursor.

zeeshankhan avatar Jun 22 '12 11:06 zeeshankhan

UITextField doesn't behave like that so I don't think this is a good idea.

gcamp avatar Jan 30 '13 17:01 gcamp

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.

cdzombak avatar Mar 07 '13 18:03 cdzombak

Hmm, did that change? Reopening this.

gcamp avatar Mar 08 '13 03:03 gcamp

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.

chrysb avatar Aug 17 '13 22:08 chrysb

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 avatar Aug 18 '13 00:08 chrysb

@chrysb can you make a pull request referring this issue?

gcamp avatar Aug 18 '13 02:08 gcamp

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

chrysb avatar Aug 18 '13 18:08 chrysb

@gcamp Do you think about integrate this to your project ?

gbero avatar Sep 06 '13 11:09 gbero