2011-09-27 44 views
0

在我的TextView有「名」如何在UITextView的(iphone)無法進行此操作

我要實現它這樣的操作..默認值爲

1)它應顯示「名字」首先
2)當我開始編輯應該成爲零
3)當我完成我的編輯,如果該領域仍然爲零,那麼它應該顯示爲「名」再次

能任何人都可以幫助我?

// *現在這是什麼,我現在在做** //

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
    replacementText:(NSString *)text 
     {  
if ([email protected]"") { 
    [email protected]"First Name(Required)"; 
    } 
// Any new character added is passed in as the "text" parameter 
if ([text isEqualToString:@"\n"]) { 
    // Be sure to test for equality using the "isEqualToString" message 

    [textView resignFirstResponder]; 

    // Return FALSE so that the final '\n' character doesn't get added 
    return FALSE; 
} 
// For any other character return TRUE so that the text gets added to the view 
return TRUE; 
    return TRUE; 
} 


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { 
NSLog(@"textViewShouldBeginEditing"); 
textView.scrollEnabled=NO; 

if (textView == first_name) { 
    first_name.text=nil; 
} 


return YES; 

}

+0

你的意思是一個UITextField?有一個名爲placeHolder的屬性。 –

+0

沒有一個textView .... –

+0

正如esqew下面說的,使用委託方法textViewDidStart/End-Editing –

回答

0

您需要在文本視圖頂部覆蓋一個UILabel,將其userInteractionEnabled設置爲NO,並在文本字段上使用委託方法shouldBeginEditing在返回YES之前隱藏UILabel並在shouldFinishEditing上隱藏它。

相關問題