I've got a few UITextFields in an UITableView. The user should be able to insert only numbers and dots. To do this, I set the keyboard type to UIKeyboardTypeNumberPad and added a '.'-Button at the bottom left corner. Every time the button is pressed, a function is called. This function should insert a dot at the current cursor position, but this is the problem: UITextField hasn't got an selectedRange property, so I'm not able to get the current cursor position. Does anybody know how to solve this problem or is there any other way to do this? Thanks.
6 Answers
I've finally found a solution for this problem! You can put the text you need inserted into the system pasteboard and then paste it at the current cursor position:
[myTextField paste:self]
I found the solution on this person's blog:
http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html
The paste functionality is OS V3.0 specific, but I've tested it and it works fine for me with a custom keyboard.
Update: As per Jasarien's comment below, it is good practice to save off the pasteboard contents first and restore them afterward. For convenience, here is my code:
// Get a reference to the system pasteboard
UIPasteboard* lPasteBoard = [UIPasteboard generalPasteboard];
// Save the current pasteboard contents so we can restore them later
NSArray* lPasteBoardItems = [lPasteBoard.items copy];
// Update the system pasteboard with my string
lPasteBoard.string = @"-";
// Paste the pasteboard contents at current cursor location
[myUIField paste:self];
// Restore original pasteboard contents
lPasteBoard.items = lPasteBoardItems;
[lPasteBoardItems release];

- 25,433
- 17
- 100
- 173
-
This is what the question actually asked. And, and excellent solution. – oberbaum May 15 '10 at 19:51
-
5This is a great solution. Though, it might be worth it to mention here that you need to save the user's pasteboard contents before putting your contents onto the pasteboard, and then restore the user's contents afterwards. This way they have no idea that you used the pasteboard and your code is a good citizen by not clobbering the contents of the user's pasteboard. – Jasarien Aug 23 '10 at 12:04
-
This solution is incompatible with VoiceOver. Instead of speaking the character inserted, it simply says "Paste". – Hampus Nilsson Sep 24 '12 at 14:41
In Swift
This inserts text at the current cursor position.
textField.insertText("Hello")
My full answer about working with the cursor position is here.
-
please add one more thing in your answer.. for case of deleting character at cursor position.. can use `deleteBackward()` this method. – Mehul Thakkar May 30 '19 at 07:04
There is now a fantastic number pad type keyboard that includes a decimal point, no configuration necessary except setting (not in IB, not an option there yet) the UITextField
's keyboardType
property to UIKeyboardStyleDecimalPad
, like so:
textField.keyboardType = UIKeyboardTypeDecimalPad;
This does all the fancy stuff you've been trying to do automatically.

- 607
- 6
- 15
If you're looking to create text fields that allow you to enter decimal places may I suggest that you rather fix the decimal point to a certain precision and allow the user to enter numbers as follows:
Assuming your precision is 2 decimal points:
start: value is 0.00
user enters 1: value is 0.01
user enters 2: value is 0.12
user enters 3: value is 1.23
user enters 4: value is 12.34
See What is the best way to enter numeric values with decimal points? for a similar solution for currency.
It is by far a more simple solution than creating a custom keyboard and dealing with all the nuances that that approach presents. If, however, you need variable length precision then that approach might suite you better.
Read the value of the text field as a string when your button is pressed. Edit the string and then set the value of the field to the new string.
-
yeah but the point is that he doesn't know how he wants to modify the string. he wants to insert a dot where the cursor is, but he doesn't know where the cursor is – newacct Aug 23 '09 at 07:55
-
This wouldn't insert a dot at the current cursor position, just at the end of the string. – WetFish Aug 24 '09 at 11:03