0

I have researched how to manually include a PlaceHolder Text in UITextView (as there are many on StackOverflow), however, whenever I click on the TextView and start typing, it does not erase the placeholder, and I am not sure why. Any help is appreciated! Thanks!

#import "HusbandryAddRecord.h"
#import <QuartzCore/QuartzCore.h>

@interface HusbandryAddRecord()

@end

@implementation HusbandryAddRecord

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 601)];

    // Round The Corners of the Notes TextView
    notesTV.clipsToBounds = YES;
    notesTV.layer.cornerRadius = 10.0f;

    placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, notesTV.frame.size.width - 20.0, 34.0)];
    [placeholderLabel setText:@"Notes"];
    [placeholderLabel setBackgroundColor:[UIColor clearColor]];
    [placeholderLabel setFont:[UIFont fontWithName:@"System" size:14.0]];
    [placeholderLabel setTextColor:[UIColor lightGrayColor]];

    [notesTV addSubview:placeholderLabel];
}

- (void)viewDidUnload
{
    [notesTV release];
    notesTV = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [notesTV release];
    [super dealloc];
}

- (void) textViewDidChange:(UITextView *)theTextView {
    if(![theTextView hasText]) {
        [theTextView addSubview:placeholderLabel];
        [placeholderLabel setText:@"Notes"];
    }

     else if ([[theTextView subviews] containsObject:placeholderLabel]) {
        [placeholderLabel removeFromSuperview];
    }
}

- (void)textViewDidEndEditing:(UITextView *)theTextView {
    if (![theTextView hasText]) {
        [theTextView addSubview:placeholderLabel];
    }
}

@end
comead
  • 577
  • 2
  • 13
  • 29
  • First thing I see wrong is that you are talking about modifying placeholder text of. UITextField but above you are defining attributes for a UILabel then applying the labels text to the text field. – Mick MacCallum Apr 11 '12 at 06:06
  • yes. UITextFields do not have a PlaceHolder. That is why my title is "Custom UITextView Placeholder." This is one of the solutions to UITextView's not having placeholders. – comead Apr 11 '12 at 06:12
  • Mine was too. Sorry about that. Haha. UITextFields do actually have PlaceHolders, but UITextViews do not. That is why I needed help to make a custom UITextView to make a Place Holder - this was accomplished by using a UILabel – comead Apr 11 '12 at 06:28
  • See, now to bad this isn't for OS X, if it was there is a built in multi-line NSTextField, which would solve your problem a lot more easily. – Mick MacCallum Apr 11 '12 at 06:30

5 Answers5

1

Whats really happening is you are adding the label to your textview for each inputing characters. So there are as many labels created one after above if you type characters and when the condition meets you remove only one label and the below lying labels are visible.

Make sure you have the delegate set properly and then use the hidden property instead of adding and removing subviews.

- (void)textViewDidChange:(UITextView *)textView
{
    placeholderLabel.hidden = textView.hasText;
}
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
ipraba
  • 16,485
  • 4
  • 59
  • 58
  • are you sure that the delegate gets called. Have you set the delegate to the textview? – ipraba Apr 11 '12 at 05:56
  • WHOOPS! haha. I accidentally set the delegate incorrectly.. Thanks for your help! It works GREAT! – comead Apr 11 '12 at 06:01
0

Use following code it will help you..

-(void)textViewDidChange:(UITextView *)textView
{
    if([textView.text length]>0){
        placeholderLabel.hidden = YES;
    }else {
        placeholderLabel.hidden = NO;
    }

}
Akshay Aher
  • 2,525
  • 2
  • 18
  • 33
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
0

You are adding placeHolderLabel on textView for entering each character.Have a look on this code:-

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[placeholderLabel removeFromSuperview];
return YES; 
}

After this write code for the time when you are done with your text writing in TextView.

-(IBAction)keyBoard:(id)sender
{
if ([textView.text length]==0) 
{
    [textView addSubview:placeholderLabel];
}
[textView resignFirstResponder];
}

this function is for button which i have took outside the textview.On click of this keyboard will be dissmiss and it will check if textView have 0 text length then placeHolderLabel will again add on it.Hope it helps.Thanks :)

Nikhil Bansal
  • 1,545
  • 13
  • 29
0

Use the lines.It works

textviewname.editable=YES; textviewname.clearsContextBeforeDrawing=YES;

0

Easy way to do : https://stackoverflow.com/a/17451491/1442541

Add label to the textView and hide when text is not empty.

Community
  • 1
  • 1
evya
  • 3,381
  • 1
  • 25
  • 28