How to determine the cursor position of a UITextField? - iphone

I have a text field that is always of the format "XX0000000XX", X being a letter, 0 being a number. I switch keyboards as the user is typing depending on whether they need to enter a number or letter.
This works fine, until the user positions the cursor in the middle of the text field to make an edit. How do I determine the cursor position within a UITextField?
I know a UITextView has a selectedRange that can be used, but I've read that you can't force a UITextView to be single line entry? i.e. Disable multiple lines of text.

Clever bit with the keyboards. My immediate thought is that perhaps that you shouldn't allow the user to move the cursor in the first place.
This isn't without precedent in the iPhone UI. The telephone keypad view, for example, restricts the user to sequential input. I have also found this technique useful for currency input. Anywhere with input that has a very rigid syntax, basically, seems like a candidate for this kind of treatment. Might work well for your situation.
Not sure if this is the best method, but here's how I do it:
A UITextField to capture input and a UILabel to display the input. The text field is hidden but is sent becomeFirstResponder to trigger the keyboard. As the user types, delegate methods do their thing to format the text, if necessary (as with currency), and update the UILabel, which provides the user feedback on their input.
Depending upon the situation, you may wish to style the UILabel in such a way that makes it clear the user can't use it as they would a selectable text field while reassuring them that it is, in fact, their input.
With 11 characters in play, I can see how curser editing might be useful. Still, phone numbers are often in that same range and I never have a problem with the sequential editor in the Phone app.

There's a UITextDelegate method -textField:shouldChangeCharactersInRange:replacementString: which should be all you need to do what you describe. Implement it to enforce whatever formatting rules you want. You shouldn't care where the cursor is, just the range of the change that's proposed.

Related

Adding line counter to a text field

I want to add a line counter to a textfield for a sort of text editor. Looking around, I've seen that a lot of questions on this topic are either unanswered, or do not answer in a satisfactory manner. Something like this How to add prefix widget on every new line in textfield in flutter? asks fundamentally the same question I am, however the only answer there is to create a new widget for each line.
This is one method that could be used, where I add new textfields in a list and build them in a row with their respective line number. However, this means I cannot have those textfields function on more than one line, and I will need to add more textfields each time a user wants to add enough text to make it overflow. The biggest flaw I see with this, though, is that it makes it harder (if not impossible?) to easily select and edit text as you would expect from a text editor, such as using the built in systems for a textfield.
Now, for my solution I am using https://pub.dev/packages/extended_text_field as a way to gain some functionality that I don't really want to take the time to figure out how to do on my own (embedded onClick callbacks for text), so it would be ideal if I could continue to use that without having to fork or even have to figure out how to write my own version.
The primary issue I'm running into is not necessarily getting the number of lines, which can be found using some solutions from How can I add line numbers to TextField on Flutter?, but rather how and where to show those line numbers.
As above, it's not really ideal to pair some number alongside a textfield and create a new textfield for each expected line. What would be ideal, however, is something like , where each line number is prefixed to the line built by the text field. For example, like this: . Something like the solution proposed in Add a prefix to every line in a multiline Text Input in Flutter? would not work especially well, because the solution there is to literally add string values to the textfield, which would require systems to clean the text, in addition to causing an unexpected ability to actually change the numbers (or other line prefix) by the user.
So -- is there any way to implement a line prefix for each line in a textfield? Would it be possible to override an InputDecoration (which allows creating the individual line prefix, but nothing more) to create a line prefix for each line drawn by the textfield? If not, exactly what would be needed to create this? I am familiar with flutter, but I am not quite as familiar with the lower level API's.

Microsoft Word: is anchor character or not?

I am trying to simulate Word's displaying of non-printing characters. There is no problem with all of them but anchors and I didn't found any info related to them. Is anchor special character placed in text or is it parameter of floating object and just displayed as special character?
Thank you for answer
The anchor, unlike most non-printing characters, can never print. It's merely a visual aid to inform the user with which paragraph or character a graphic with text flow formatting is associated. It's not possible to detect an anchor directly in the document text using Word's API (object model). It's bound to the graphic and would require analyzing the properties of the Shape object.
It could be determined by analyzing the document's WordOpenXML, although the term "anchor" is not used. The information could be deduced from the location and settings of the nodes that define where and how the graphic appears.
Is anchor special character placed in text or is it parameter of floating object and just displayed as special character?
I'm going to try to answer the "is it in text" question.
If, while debugging, you try to get a textual character for an anchor from a range's text, it won't be there. There won't even be a 0-width non-visible character there, like when you move a text cursor to the right past a non-printable character, but it doesn't actually move because there's something there (this may be editor-dependent, I have Notepad++ in mind).
So no, it's not in text.
But, at the same time, it will interfere with searches. E.g. If you put the word "text" on a line, put a text box on that line to create an anchor, and then search for "^13text" (with wildcards enabled, ^13 means the end-of-paragraph mark), it won't find it.
So yes, it must be in text because it interferes with searches.
So this might be a contradiction, but let's keep going. If it's in text, where is it? If you place the text cursor on the previous line, hold shift, and move it once to the right, the text box will be highlighted.
So it must be at the start.
But, there is also evidence that contradicts this. If you have a field at the start of the line with text on it, you can move it once to the right as before, and then once to the left, and though you have part of the line highlighted, the text box won't be part of the selection.
So, I really have no idea whether it's text or not, or where it is if so, but hopefully this helps someone else.

iOS UITextField formatted for time

I have a UITextField which I created programmatically. This text field is used to input a time. The placeholder text is "00:00:00.000". When the text field is tapped, a number pad will appear. Ideally, I want the gray place holder text to remain as the user types. As they enter a number, it replaces the gray zeros in the place holder text and skips over the colons and period (leaving the format intact without the user entering any punctuation). If it is not possible to keep the place holder text and replace it as the user types, I would like the punctuation to automatically be entered.
Thanks in advance!
iOS has a UIDatePicker control for this, if you want to go the way you are going then you will have handle all the user input your self.

How can I automatically enable the shift key?

When the user types in my text field, I want the first letter of every word to be capitalised. One way of doing this is use textField.autocapitalisationType, which autocorrects every word after the user has typed it - but this method is causing separate problems for me (see my other question).
The Contacts app uses a different approach - when the user types a name, the shift key auto-highlights at the start of every word. How can I do this?
This should result in the text field capitalizing each word:
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
Alternatively, if you made the text field in Interface Builder, you could set its Capitalization property to Words.
From the iOS Developer Library documentation:
This property determines at what times the Shift key is automatically
pressed, thereby making the typed character a capital letter. The
default value for this property is
UITextAutocapitalizationTypeSentences.
I believe this describes what you are looking for. The value you want is UITextAutocapitalizationTypeWords.

textfield animation for a specific character during input

I've got a textfield, which is using a numberpad for input, and I'd like to make it so that when the user types in a number, the textfield automatically inserts an ' (apostrophe) after the first digit, and any other numbers would follow the apostrophe. But to make this noticeable, I'd like to have the apostrophe 'pop up' with a change of color, before resting in the textfield. Much like the action of the keyboard letters when they're hit - they pop out for a second.
Seems to me there's several things to be done and I haven't found any help online with them:
Get notice after one digit is entered (more than one digit could be entered, but the animation would take place and the additional numbers would show up after the apostrophe).
Add the apostrophe into the textfield.
Animate the insertion of the apostrophe, while allowing the textfield to continually take in input.
Any direction to libraries or code or suggestions would be immensely appreciated. Thank you so much. I've a hunch I'm supposed to use coco2d but have no idea how to get started.
I figured out a work around that I'd thought I'd share in case anyone is interested. For monitoring input, you can use:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
which gets called when the user touches the keyboard, but before the character or number is processed and put to screen. The only problem is that on an empty text field, if the user hits backspace this method doesn't get called (even if not null). Basically because there's no change to an empty text field to process with an empty string.
You could also use: UITextFieldTextDidChangeNotification, but it gets called in the same way and doesn't offer any parameters to play with.
For 2: I ended up making labels for these and putting them over the text field where I wanted, and playing with their properties as needed. It was kind of messy so I actually split the text field into 2 and used the above method to jump in between. Seems to work mostly well, but dealing with the backspace issue is still unsolved (how to move back to the first text field when backspacing through text for example). Am considering overlaying a transparent button on that part of the keyboard. Seems messy though. I would appreciate any cleaner ideas that are out there.
So, didn't end up doing any animations, which would have been a much slicker. Hope this helps.