Placeholder in UITextView

前端 未结 30 3160
野趣味
野趣味 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:02

    Sorry to add another answer, But I just pulled something like this off and this created the closest-to-UITextField kind of placeholder.

    Hope this helps someone.

    -(void)textViewDidChange:(UITextView *)textView{
        if(textView.textColor == [UIColor lightGrayColor]){
            textView.textColor  = [UIColor blackColor]; // look at the comment section in this answer
            textView.text       = [textView.text substringToIndex: 0];// look at the comment section in this answer
        }else if(textView.text.length == 0){
            textView.text       = @"This is some placeholder text.";
            textView.textColor  = [UIColor lightGrayColor];
            textView.selectedRange = NSMakeRange(0, 0);
        }
    }
    
    -(void)textViewDidChangeSelection:(UITextView *)textView{
        if(textView.textColor == [UIColor lightGrayColor] && (textView.selectedRange.location != 0 || textView.selectedRange.length != 0)){
            textView.selectedRange = NSMakeRange(0, 0);
        }
    }
    

提交回复
热议问题