【问题标题】:Placeholder in UITextView label does not disapearUITextView 标签中的占位符不会消失
【发布时间】:2023-03-27 09:44:01
【问题描述】:

我知道这里有很多用于 UITextView 占位符的链接,我尝试了一个对我有用的链接,但我没有用。即使我开始在 textView 中输入,它们的占位符文本也会一直存在。有人知道为什么吗?我在下面粘贴了我的代码。

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];
 }

@结束

【问题讨论】:

    标签: objective-c xcode4.2 uitextview contentplaceholder


    【解决方案1】:

    我相信textViewDidChange: 只有在编辑完成后才会被调用。您可能想隐藏 textViewDidBeginEditing: 上的占位符。

    【讨论】:

    • 我不确定你的意思是什么?
    • 当用户在 UITextView 中输入文本时,您粘贴的代码不起作用。 - (void) textViewDidChange:(UITextView *)theTextView { ...} 仅在他们选择另一个字段或隐藏键盘等后才会被调用。textViewDidBeginEditing: 将在用户开始输入该 UITextView 时立即被调用。
    • 这是有道理的。我想出了另一种方法来做到这一点。我刚刚向UITextViewTextDidBeginEditingNotification 添加了一个观察者,并创建了一个删除占位符的函数,一旦该通知被调用:)
    猜你喜欢
    • 2016-01-29
    • 2018-12-30
    • 2010-11-22
    • 2011-07-17
    • 2012-08-02
    • 2015-12-03
    • 2016-06-21
    • 2018-10-08
    • 2014-09-06
    相关资源
    最近更新 更多