Я исследовал, как вручную включать текст PlaceHolder в UITextView (как и многие из StackOverflow), однако, всякий раз, когда я нажимаю TextView и начинаю вводить текст, он не стирает местозаполнитель , и я не уверен, почему. Любая помощь приветствуется! Благодаря!Пользовательский UITextView PlaceHolder не стирается при вводе клавиатуры
#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, а затем применяете текст меток в текстовое поле. –
есть. У UITextFields нет PlaceHolder. Вот почему мое название - «Custom UITextView Placeholder». Это одно из решений, что UITextView не имеет заполнителей. – comead
Опечатка, опечатка. Игнорирование. –