How to use UITextRange to get substring from a UITextField - iphone

So I am using selectedTextRange to get a cursor position from my UITextField. I would like to obtain a substring from the beginning of my TextField text up to the cursor position. How would I go about doing this? I am able to create the UITextRange I want but I don't know how to use this to get the actual substring since substringWithRange seems to only work with NSRange. Any guidance would be greatly appreciated.

You need to use the methods from the UITextInput protocol, which is implemented by all text views, to do this. With a text range, you can simply call textInRange: to get the text.
To get the text from the beginning of the field up to the selected position, you'll need to make a text range first with textRangeFromPosition:toPosition:, passing beginningOfDocument for the first argument, and the start of your text range for the second argument.

Related

TextField's current focus position in flutter

Hey I have a TextField and a controller for it through which I access its current text value and add some text like (Some Special Asterix or text with angular brackets just like hashnode's text editor) to it when needed, adding text at the end is easy.
controller.text += "Something";
The above code will add Something to the end. But I need to know the current TextFields Cursor position and add text according to it.
I really love to know how we can do it in flutter efficiently
Try this to get the cursor position
print(controller.selection.baseOffset);
print(controller.selection.extentOffset);
if they are the same, it is current cursor position.
if not, it means that some of text is selected, baseOffset is start position and extentOffset is end positon.

How to format different characters in a UILabel's string differently?

I have a UILabel with a dynamic string as the text property. If I always want the first word to be underlined, do I need to separate labels and then try to line them up correctly? Or could I subclass UILabel to have it do this?
Use a UIWebView and render the text as HTML.
I ended up using the textRectForBounds:limitedToNumberOfLines: to find the dynamic start point of the word (minus some x coordinate pixels), and then took away the first letter of the uilabel with stringByReplacingCharactersInRange:withString:, and then added another uilabel with the beginning letter of the original label, just with different font.

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.

How to figure out the current caret position in a UITextField?

I want to programmatically paste a string into a text field or text view at the current caret position.
Is there an easy way to do this? I would need to know the current caret position, but there's no method to retrieve it, right? Don't want to call private API. Is there any legal way to do it?
Use UITextView's selectedRange method. If the range has a non-zero length (in the case of the user having selected some text), just take the location (or NSMaxRange(), or replace the entire range...)
Instead of pasting option. Get the string from textfield or textview insert the string which you want to paste it by providing the index and then clear the textfield then pass the string which you inserted.

How to determine the cursor position of a UITextField?

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.