UItextView设置提示性文字

iOS 的textView有没有自带的设置提示文字的方法,我试过在上面加一个UILabel可以实现,但是觉得有点麻烦,求大神指教一下!

阅读 6.5k
2 个回答

自己写个逻辑


// tips是提示文本

- (void)addTextView {
    UITextView *textView = [UITextView new];
    textView.text = tips;
    textView.textColor = [UIColor grayColor];
    _inputContent = @"";
    ………………省略
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([_inputContent isEqualToString:@""]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    } else {
        textView.textColor = [UIColor blackColor];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text && [textView.text length] > 0) {
        _inputContent = textView.text;
        textView.textColor = [UIColor blackColor];
    } else {
        _inputContent = @"";
        textView.text = tips;
        textView.textColor = [UIColor grayColor];
    }
    
}

textView里显示的是表象,_inputContent相当于你的数据源

注意placeholder是自定义的添加到texView即可

- (void)paste:(id)sender
{
    
    UIPasteboard *board = [UIPasteboard generalPasteboard];
    self.textView.text = board.string;
    [self resignFirstResponder];
}

-(void)keyboardHideClick:(UITapGestureRecognizer*)tap{
    
    [self.textView resignFirstResponder];
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{    if (![text isEqualToString:@""])
    
{
    
    _placeholderLabel.hidden = YES;
    
}
    
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
        
    {
        
        _placeholderLabel.hidden = NO;
        
    }
    
    return YES;
    
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题