0
我知道這裏有很多關於UITextView佔位符的鏈接,我嘗試了爲我工作的那個,但我不工作。即使當我開始在textView中輸入時,他們的佔位符文本仍然存在。任何人有任何想法,爲什麼?下面粘貼了我的代碼。UITextView標籤中的佔位符不會消失
AddExerciseViewController.m
#import "AddExerciseViewController.h"
@implementation AddExerciseViewController
@synthesize nameTextField = _nameTextField;
@synthesize descriptionTextView = _descriptionTextView;
@synthesize placeholderLabel = _placeholderLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
_descriptionTextView.layer.borderWidth = 3.0f;
_descriptionTextView.layer.borderColor = [[UIColor grayColor ] CGColor];
_descriptionTextView.layer.cornerRadius = 5;
_descriptionTextView.clipsToBounds = YES;
_placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, _descriptionTextView.frame.size.width - 20.0, 34.0)];
[_placeholderLabel setText: @"FooBar"];
// placeholderLabel is instance variable retained by view controller
[_placeholderLabel setBackgroundColor:[UIColor clearColor]];
[_placeholderLabel setTextColor:[UIColor lightGrayColor]];
[_descriptionTextView addSubview:_placeholderLabel];
[super viewDidLoad];
}
- (void) textViewDidChange:(UITextView *)theTextView
{
if(![_descriptionTextView hasText]) {
[_descriptionTextView addSubview:_placeholderLabel];
[UIView animateWithDuration:0.15 animations:^{
_descriptionTextView.alpha = 1.0;
}];
} else if ([[_descriptionTextView subviews] containsObject:_placeholderLabel]) {
[UIView animateWithDuration:0.15 animations:^{
_placeholderLabel.alpha = 0.0;
} completion:^(BOOL finished) {
[_placeholderLabel removeFromSuperview];
}];
}
}
- (void)textViewDidEndEditing:(UITextView *)theTextView
{
if (![_descriptionTextView hasText]) {
[_descriptionTextView addSubview:_placeholderLabel];
[UIView animateWithDuration:0.15 animations:^{
_placeholderLabel.alpha = 1.0;
}];
}
}
- (void)viewDidUnload
{
[self setDescriptionTextView:nil];
[self setNameTextField:nil];
[super viewDidUnload];
}
@end
我不確定你的意思是什麼? – 2012-03-21 22:52:12
您粘貼的代碼在用戶將文本輸入到UITextView時不會工作。 ' - (void)textViewDidChange:(UITextView *)theTextView {...}'只有在選擇另一個字段或隱藏鍵盤之後纔會被調用。 'textViewDidBeginEditing:'將在用戶開始輸入時立即調用UITextView的。 – MechEthan 2012-03-21 23:25:22
這很有道理。我想通過另一種方式來做到這一點。我只是添加了一個觀察者'UITextViewTextDidBeginEditingNotification',並且這個函數刪除了佔位符,一旦該通知被調用:) – 2012-03-21 23:34:16