我已經研究如何手動包括在UITextView中的佔位符文本(因爲有許多在計算器上),然而,每當我點擊的TextView並開始打字,它不會刪除佔位,我不知道爲什麼。任何幫助表示讚賞!謝謝!自定義的UITextView佔位符不刪除在鍵盤輸入
#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
我看錯第一件事是,你在談論修改的佔位符文本。 UITextField,但上面的是爲UILabel定義屬性,然後將標籤文本應用於文本字段。 – 2012-04-11 06:06:16
是的。 UITextFields沒有PlaceHolder。這就是爲什麼我的標題是「自定義UITextView佔位符」。這是UITextView沒有佔位符的解決方案之一。 – comead 2012-04-11 06:12:59
對不起,錯字。漠視。 – 2012-04-11 06:18:44