0

我研究了如何在 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
4

5 回答 5

1

真正发生的是您正在为每个输入字符添加标签到您的文本视图中。因此,如果您键入字符,并且在满足条件时仅删除一个标签并且下面的标签可见,则会在上面创建尽可能多的标签。

确保您正确设置了委托,然后使用隐藏属性而不是添加和删除子视图。

- (void)textViewDidChange:(UITextView *)textView
{
    placeholderLabel.hidden = textView.hasText;
}
于 2012-04-11T05:49:17.623 回答
0

使用线条。它有效

textviewname.editable=YES;textviewname.clearsContextBeforeDrawing=YES;

于 2012-04-11T11:41:00.950 回答
0

简单的方法:https ://stackoverflow.com/a/17451491/1442541

将标签添加到 textView 并在文本不为空时隐藏。

于 2013-07-03T15:08:22.170 回答
0

使用下面的代码它会帮助你..

-(void)textViewDidChange:(UITextView *)textView
{
    if([textView.text length]>0){
        placeholderLabel.hidden = YES;
    }else {
        placeholderLabel.hidden = NO;
    }

}
于 2012-04-11T05:43:00.790 回答
0

您在 textView 上添加 placeHolderLabel 以输入每个字符。查看此代码:-

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[placeholderLabel removeFromSuperview];
return YES; 
}

在此之后为您在 TextView 中完成文本编写时编写代码。

-(IBAction)keyBoard:(id)sender
{
if ([textView.text length]==0) 
{
    [textView addSubview:placeholderLabel];
}
[textView resignFirstResponder];
}

此功能适用于我在 textview 之外使用的按钮。单击此键盘时将被取消,它将检查 textView 是否有 0 文本长度,然后 placeHolderLabel 将再次添加它。希望它有所帮助。谢谢 :)

于 2012-04-11T10:59:15.083 回答