简体   繁体   English

如何将插入符号(光标)移动到可可中的行尾

[英]How to move caret (cursor) to the end of the line in cocoa

This is probably an easy task, but Im new to cocoa, and haven't found the solution yet. 这可能是一项简单的任务,但我是cocoa的新手,还没有找到解决方案。

The algorithm I wrote accepts and validates user's input. 我写的算法接受并验证用户的输入。 It accepts only numbers and then takes the input to the acceptable form, which is nn:nn:nn where nn is a two digit number. 它只接受数字,然后将输入转换为可接受的形式,即nn:nn:nn其中nn是两位数字。

The way the algorithm works is following: 算法的工作方式如下:

  1. accept the digit 接受数字
  2. check if we need to insert : 检查我们是否需要插入:
  3. add to final string 添加到最终字符串

So what happens is that after user typed in 123 in a text field it looks like: 12:|3 (where | is the cursor position) 那么在用户在文本字段中输入123之后会发生以下情况:12:| 3(其中|是光标位置)

As you can understand if user types 12345 for example the input will look like: 12:45:|3 which is wrong. 您可以理解,如果用户输入12345,例如输入将如下所示:12:45:| 3这是错误的。

I see the fix for this as just a simple move of the cursor to the end of the line after I do the : insertion. 我看到修复此问题只是在执行:insert之后将光标简单移动到行尾。 But I can't find the way to do it so far. 但到目前为止我找不到办法。 :( :(

If anyone can help me to fix this bug, I would really appreciate it! 如果有人可以帮我修复这个bug,我真的很感激!

Thank you! 谢谢!

You can try these : 你可以试试这些:

[[self.inputFileTextField currentEditor] moveToEndOfLine:nil];

If you have textView then, 如果你有textView然后,

moveToEndOfParagraph:

or 要么

NSTextView * textView = [thePanel textView forObject:theTextField];

NSUInteger text_len = [[textView string] length];

[textView setSelectedRange:(NSRange){text_len, 0}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM