1.在viewDidLoad中添加一個(gè)默認(rèn)字體的Lable
placeHoderLable = [[UILabel alloc]initWithFrame:CGRectMake(3,3, DeviceWidth-6,40)];//根據(jù)情況調(diào)節(jié)位置placeHoderLable.enabled=NO;
placeHoderLable.text=@"請(qǐng)?jiān)谶@里輸入您要預(yù)測(cè)的內(nèi)容遂跟。";
placeHoderLable.numberOfLines=0;
placeHoderLable.font=? [UIFontsystemFontOfSize:12];
placeHoderLable.textColor= [UIColorlight GrayColor];
[_myTextView addSubview:placeHoderLable];
_myTextView.delegate=self;//記得設(shè)置textview的代理_myTextView.returnKeyType=UIReturnKeyDone;
2.在textView的代理方法中實(shí)現(xiàn)以下代碼
-(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;
? }
}