我研究了如何在 UITextView 中手动包含 PlaceHolder Text(因为 StackOverflow 上有很多),但是,每当我单击 TextView 并开始输入时,它都不会删除占位符,我不知道为什么。任何帮助表示赞赏!谢谢!
#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