Placeholder in UITextView

前端 未结 30 3088
野趣味
野趣味 2020-11-22 16:01

My application uses an UITextView. Now I want the UITextView to have a placeholder similar to the one you can set for an UITextField.<

30条回答
  •  悲&欢浪女
    2020-11-22 16:13

    Here's yet another way to do it, one that reproduces the slight indentation of UITextField's placeholder:

    Drag a UITextField right under the UITextView so that their top left corners are aligned. Add your placeholder text to the text field.

    In viewDidLoad, add:

    [tView setDelegate:self];
    tView.contentInset = UIEdgeInsetsMake(-8,-8,0,0);
    tView.backgroundColor = [UIColor clearColor];
    

    Then add:

    - (void)textViewDidChange:(UITextView *)textView {
        if (textView.text.length == 0) {
            textView.backgroundColor = [UIColor clearColor];            
        } else {
            textView.backgroundColor = [UIColor whiteColor];
        }
    }
    

提交回复
热议问题