建立一個UITextView,預設啟動鍵盤,並將游標定位到首位置,因為UITextFiled類沒有此功能,所以改用UItextView.
代碼如下:
[cpp] UITextView *m_contentTextField = [[[UITextView alloc] init] autorelease];
m_contentTextField = [[[UITextView alloc] init] autorelease];
m_contentTextField.frame = CGRectMake(0, 0, 320, 90) ;
m_contentTextField.backgroundColor = [UIColor whiteColor] ;
m_contentTextField.font = [UIFont systemFontOfSize:14];
m_contentTextField.delegate = self ;
UITextView *m_contentTextField = [[[UITextView alloc] init] autorelease];
m_contentTextField = [[[UITextView alloc] init] autorelease];
m_contentTextField.frame = CGRectMake(0, 0, 320, 90) ;
m_contentTextField.backgroundColor = [UIColor whiteColor] ;
m_contentTextField.font = [UIFont systemFontOfSize:14];
m_contentTextField.delegate = self ;
設定此UITextView為第一響應者,www.2cto.com 即預設開啟鍵盤。
[cpp] [m_contentTextField becomeFirstResponder];
[m_contentTextField becomeFirstResponder];當UITextView中含有文字時,系統預設將游標定位到最後的位置,下面的語句將游標定位到首位置。
[cpp] m_contentTextField.selectedRange = NSMakeRange(0,0);
摘自 zcl369369的專欄