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.
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];
- (void)insertText:(NSString *)text
available in iOS 3.2 and later.
In Swift
This inserts text at the current cursor position.
textField.insertText("Hello")
My full answer about working with the cursor position is here.
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.
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.
Related
I have some alphanumeric content that gets entered routinely and is mostly numbers, so I'm wondering if there's a way to start in the numeric side of the keyboard but still have the option to switch to the alphabetic keyboard in Objective-C for iPhone development specifically. I've found plenty of info on using the numeric keyboard but I'd like to start there but still be able to enter letters if that's possible.
Thanks.
Set the keyboardType property to UIKeyboardTypeNumbersAndPunctuation.
This will default the iPhone's keyboard to show numbers and punctuation but the little "ABC" key will be in the bottom left allowing the user to switch back to the letters (and then back to numbers and punctuation).
Try this:
[Objective - C]
[self.myTextField setKeyboardType: UIKeyboardTypeNumbersAndPunctuation];
[Swift]
self.myTextField.keyboardType = .NumbersAndPunctuation
// You can also set this property from the storyboard.
I want to know essentially what this guy is asking how to detect when the iOS default keyboard type switches from text to numbers
I want to know how to know when the user switches from the alphabet side of the Default keyboard to the Numeric side.
The reason I want that is that I want to keep the numeric side when user changes from one textfield to the next, if the last thing they wrote is a number.
And I can't use the numbersAndPunctuation keyboard type as a whole because it doesn't have the ability to change to Emoji, which I still need to be able to.
But if I knew when they switched, I would be able to start at the NumbersAndPunctuation and then when the user switched I would be able to change keyboard type to default, so the User would be able to switch to Emoji.
Either that or the ability to start the default keyboard on the numbers side. If that's possible that would be a solution aswell.
Any help would be appreciated :)
Here is my contribution to the "detecting" the keyboardType. Essentially, there is no API to get what you want. https://stackoverflow.com/a/26095686/2719509
How to get the functionality you want with the current API.
The user types text into the [original] textField, changes the
keyboard, wants to go to the next keyboard
Create a [new]text field, populate it with the text the user
entered, position it in exactly the same position of the text field
where the user was working
Move the [original]textField to the position of the
[next]textField, remove the text entered and replace it with the
text of the[next]textField. Make the [next]textField.hidden = YES
until the user goes to the [next next]textField.
Repeat 2 & 3.
Don't use animations while moving textFields around.
What's the trick: to preserve the state of the keyboard keep the textField constant and make the user believe he is writing in different textFields.
Hope it helps.
add if (textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation) in your textfield delegates
Try this, it will help you detect the last character, using this you can identify keyboard type. (presently which type of keyboard is opened?)
- (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters{
if([characters rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location == NSNotFound){
NSLog(#"decimalDigitCharacterSet");
}else if([characters rangeOfCharacterFromSet:[[NSCharacterSet letterCharacterSet] invertedSet]].location == NSNotFound){
NSLog(#"letterCharacterSet");
}
return YES;
}
It's working for me.. Hope this will help you.
I have an input field the user needs to fill with an alphanumeric code. The keyboard the user uses to type the code has a dynamic return button that changes to "send" as he writes some text on the field. When the field is empty the return button has the default value.
To dynamically change the return button type I use the following code:
if([textField.text isEqualToString:#""])
{
textField.returnKeyType = UIReturnKeyDefault;
}
else
{
textField.returnKeyType = UIReturnKeySend;
}
[textField reloadInputViews];
However this has the following drawback: since the code is alphanumeric, the user may be typing numbers, and yet the keyboard will always switch back to the letter keyboard, so to type more than one number in a row he will need to be continuously switching to number keyboard.
Is there any way to dynamically change the return key of a keyboard as the user types but to preserve the keyboard state to letters or numbers keyboard?
I think this is not a bug on Apple's side, more a missing implementation of API for the keyboard. With the new iOS8 API you might want to create your own keyboard returning the UIKeyboardType.
For iOS7 I worked around by inspecting the views of the keyboard. Use the US2KeyboardType CocoaPod or the source:
https://github.com/ustwo/US2KeyboardType
As Martin noted above, it's not a bug on Apple's side, but on my side. However I'll be posting the solution I've found since it is the one that solves that particular problem:
Instead of manually changing the return key type when there is text on the text field, Apple provides us with a property called enablesReturnKeyAutomatically that when set to YES it automatically manages the issue, enabling and disabling the return key depending on whether there is text or not in the text field.
Therefore you don't need to modify the returnKeyType property, and thus, no calling to reloadInputViews is required, so the keyboard doesn't change back to its original state.
Is there a native UI control for code input text field, for example like Whatsapp:
No. To achieve this, they're almost certainly tapping into the textField:shouldChangeCharactersInRange:replacementString: method for their UITextField, selectively accepting and formatting user input to match the dash-if-empty approach.
Further, I'm sure they've subclassed the field; per your comments there isn't a blue cursor - which isn't standard for a UITextField.
No there isn't. Use a UITextField, fill it with dashes, keep track of how many characters the user has entered, and replace the dashes accordingly as the user types.
There's a 4-digit code input text field called CodeInputView written in Swift.
In the past I've added a UITextField to the view and set its hidden == true. Then I show/hide the keyboard by calling becomeFirstResponder()/resignFirstResponder() on it. I listen for text did change notifications and update a visible label with the value of the hidden text field.
My aim is to have text boxes - a set amount per level for people to guess a hidden word. I don't want the UITextfield to be tapped and then bring up the keyboard, I'd like to have a different button that brings up the keyboard - if that's possible.
If each box is a separate text field how could I go about entering text. When a user types on the standard apple keypad, how could each character be inputted into a certain text field. I'd preferably like the text to show in the box as soon as a key is tapped.
I'm also having trouble clearing certain letters. Say a user mis-spells something and doesn't realise until the keyboard has resigned as first responder, how could I make it so that a user can tap on maybe two boxes if the rest of the word is spelt right and the program clear it?
Is there any way of writing the program so that it inputs text only if the text field is empty? Continuing with the example above they switch two letters, they tap to clear, they then bring up the keypad and the next key then pressed fills the empty boxes. Not allowing the program to input text in a used text field that only contains a single character?
I'm using Cocos2d - I don't know if that makes a difference. I hope you understand what I mean, although I'm rather bad at explaining.
Thank you in advance for your time and any help :).
Instead of having a textbox that you don't want users to edit, why not use a label?
To show the keyboard, you need to have it linked to some UITextField (or similar). You could use an invisible UITextField, and then monitor the input and send the characters to the correspinding labels. Refer to this question.
To check if a textfield has anything in it:
[textField.text length] > 0, but I would use labels instead of textfields.
I dont really understand the other parts of your question.