自定义UITextView PlaceHolder在键盘输入时没有擦除

时间:2012-04-11 05:34:57

标签: iphone objective-c ios uitextview

我研究过如何在UITextView中手动包含PlaceHolder文本(因为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

5 个答案:

答案 0 :(得分:1)

真正发生的是您为每个输入字符添加标签到textview。因此,如果您键入字符,则会在上面创建一个标签,并且当条件符合您时,只删除一个标签,并且下面的标签可见。

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

- (void)textViewDidChange:(UITextView *)textView
{
    placeholderLabel.hidden = textView.hasText;
}

答案 1 :(得分:0)

使用以下代码可以帮助您..

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

}

答案 2 :(得分: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将再次添加它。希望它有所帮助。谢谢:)< / p>

答案 3 :(得分:0)

使用线条。它有效

<强> textviewname.editable = YES;  textviewname.clearsContextBeforeDrawing = YES;

答案 4 :(得分:0)

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

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