1.在viewDidLoad中添加一个默认字体的Lable
placeHoderLable = [[UILabel alloc]initWithFrame:CGRectMake(3,3, DeviceWidth-6,40)];//根据情况调节位置placeHoderLable.enabled=NO;
placeHoderLable.text=@"请在这里输入您要预测的内容。";
placeHoderLable.numberOfLines=0;
placeHoderLable.font= [UIFontsystemFontOfSize:12];
placeHoderLable.textColor= [UIColorlight GrayColor];
[_myTextView addSubview:placeHoderLable];
_myTextView.delegate=self;//记得设置textview的代理_myTextView.returnKeyType=UIReturnKeyDone;
2.在textView的代理方法中实现以下代码
-(void)textViewDidChange:(UITextView*)textView
{ if([_myTextView.textlength] ==0) {
[placeHoderLable setHidden:NO];
}else {
[placeHoderLable setHidden:YES];
}
}
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString*)text
{
if([text isEqualToString:@"\n"]|| ([text isEqualToString:@""])
{
[textView resignFirstResponder];
returnNO;
}
if(range.location>=200){
_yuNumLable.text=@"还能输入0字" ;
returnNO;
}else{
_yuNumLable.text=[NSStringstringWithFormat:@"还能输入%lu字",200-range.location];
returnYES;
}
}