0

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
4

5 に答える 5

1

実際に起こっていることは、入力文字ごとにテキストビューにラベルを追加していることです。したがって、文字を入力し、条件が満たされたときに1つのラベルのみを削除すると、上に次々に作成されるラベルがいくつもあり、下にあるラベルが表示されます。

デリゲートが適切に設定されていることを確認してから、サブビューを追加および削除する代わりに、hiddenプロパティを使用してください。

- (void)textViewDidChange:(UITextView *)textView
{
    placeholderLabel.hidden = textView.hasText;
}
于 2012-04-11T05:49:17.623 に答える
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 に答える
0

次のコードを使用してください。

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

}
于 2012-04-11T05:43:00.790 に答える
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 に答える