第一种:
1.在创建textView的时候,赋值其文本属性
即textView.text = @"placeholder";
2.在开始编辑的代理方法中进行如下操作
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView.text isEqualToString:@"placeholder"]) {
textView.text = @"";
}
}
3.在结束编辑的代理方法中进行如下操作
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView.text.length<1) {
textView.text = @"placeholder";
}
}
第二种:
解释: 这里面的self.questionView 就是textView,_textViewPlacehodelLabel 是placehoder
这算是自己写的 在代理方法 中(包括了键盘监听)
- (
void
)touchesBegan:(
NSSet
<
UITouch
*> *)touches withEvent:(
UIEvent
*)event{
if (![ self . questionView isExclusiveTouch ]) {
[ self . questionView resignFirstResponder ];
}
}
- ( void )textViewDidChange:( UITextView *)textView{
if ( self . questionView . text . length != 0 ) {
_textViewPlacehodelLabel . hidden = YES ;
} else {
_textViewPlacehodelLabel . hidden = NO ;
}
if (![ self . questionView isExclusiveTouch ]) {
[ self . questionView resignFirstResponder ];
}
}
- ( void )textViewDidChange:( UITextView *)textView{
if ( self . questionView . text . length != 0 ) {
_textViewPlacehodelLabel . hidden = YES ;
} else {
_textViewPlacehodelLabel . hidden = NO ;
}
}