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
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.