iPhone开发--光标定位到含有文字的UITextVIew的首位置

来源:互联网 发布:淘宝卖家后台进不去 编辑:程序博客网 时间:2024/05/22 15:11

建立一个UITextView,默认启动键盘,并将光标定位到首位置,因为UITextFiled类没有此功能,所以改用UItextView.

代码如下:

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 becomeFirstResponder];
当UITextView中含有文字时,系统默认将光标定位到最后的位置,下面的语句将光标定位到首位置。
m_contentTextField.selectedRange = NSMakeRange(0,0);

参考文献:https://discussions.apple.com/message/12209784#12209784

原创粉丝点击