【问题标题】:TextView Placeholder in ios objective cios目标c中的TextView占位符
【发布时间】:2017-01-14 16:06:00
【问题描述】:

我在 iOS 应用程序中使用 textview,但是当我在我的 textview 中设置占位符时我遇到了一些问题,当我在 textview 中单击时文本不会隐藏,所以这里的任何人都可以在 textview 中使用占位符

【问题讨论】:

标签: objective-c iphone ios7


【解决方案1】:

UITextView 中不能创建占位符,但是可以通过这个生成占位符的效果。

- (void)viewDidLoad{
    commentTxtView.text = @"Comment";
    commentTxtView.textColor = [UIColor lightGrayColor];
    commentTxtView.delegate = self;
}

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
    commentTxtView.text = @"";
    commentTxtView.textColor = [UIColor blackColor];
    return YES;
}

-(void) textViewDidChange:(UITextView *)textView
{
    if(commentTxtView.text.length == 0){
        commentTxtView.textColor = [UIColor lightGrayColor];
        commentTxtView.text = @"Comment";
        [commentTxtView resignFirstResponder];
    }
}

-(void) textViewShouldEndEditing:(UITextView *)textView
{
    if(commentTxtView.text.length == 0){
        commentTxtView.textColor = [UIColor lightGrayColor];
        commentTxtView.text = @"Comment";
        [commentTxtView resignFirstResponder];
    }
}

或者你可以像在文本视图中添加标签一样

lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,textView.frame.size.width - 10.0, 34.0)];


[lbl setText:kDescriptionPlaceholder];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
textView.delegate = self;

[textView addSubview:lbl];

设置

- (void)textViewDidEndEditing:(UITextView *) textView
{
     if (![textView hasText]) {
         lbl.hidden = NO;
     }
}

- (void) textViewDidChange:(UITextView *)textView
{
    if(![textView hasText]) {
       lbl.hidden = NO;
    }
    else{
       lbl.hidden = YES;
    }  
}

【讨论】:

    【解决方案2】:

    试试这个

    1.创建标签并显示或隐藏

    -(void)viewdidLoad
    {
       UILabel*placeHolderLabel = [UILabel alloc]initWithFrame:CGRectMake(0,0,300,30)];
       placeHolderLabel.text = @"PlaceHolderText";
       placeHolderLabel.textColour = [UIColour lightGrayColor]; 
       [yourTextView addSubView:placeHolderLabel];
    }
    
    //2.TextViewDelegate
    
    - (void)textViewDidBeginEditing:(UITextView *)textView
    {
        placeHolderLabel.hidden = YES;
        [textView becomeFirstResponder];
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView
    {
        if ([textView.text isEqualToString:@""]) {
            placeHolderLabel.hidden = NO;
        }
        [textView resignFirstResponder];
    }
    

    【讨论】:

      【解决方案3】:

      注意:文本视图中没有任何用于占位符的内置函数,如文本字段。

      .h 文件中为您的textView 创建一个IBOutlet,如下所示

      并添加<UITextViewDelegate>,如下所示

      #import <UIKit/UIKit.h>
      
      @interface ViewController : UIViewController<UITextViewDelegate>
      @property (weak, nonatomic) IBOutlet UITextView *mytextView;
      
      
      @end
      

      然后在您的.m 文件中,实现textview delegate 方法,如下所示。

      如果你想要一些补充,也可以用上面的代码尝试下面的方法

      - (void)textViewDidChange:(UITextView *)textView
      {
          NSString *mtvt = textView.text;
          if ([mtvt isEqualToString:@""]) {
              self.mytextView.text = @"Your message here";
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用 textView 委托方法以编程方式管理占位符文本

        - (void)textViewDidBeginEditing:(UITextView *)textView
        {
            if ([textView.text isEqualToString:@"myPlaceHolderText"])       
            {
             textView.text = @"";
            }
            [textView becomeFirstResponder];
        }
        
        - (void)textViewDidEndEditing:(UITextView *)textView
        {
            if ([textView.text isEqualToString:@""]) {
                textView.text = @"myPlaceHolderText";
            }
            [textView resignFirstResponder];
        }
        

        【讨论】:

          猜你喜欢
          • 2011-08-24
          • 2011-03-30
          • 2021-07-22
          • 2018-03-05
          • 2015-12-03
          • 2010-11-01
          • 2018-03-21
          • 1970-01-01
          • 2018-06-07
          相关资源
          最近更新 更多