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.
Related
Right now when the keyboard launches on my application it defaults to the letter side showing an alphabetical keyboard.
Question will be listed below images
Refer to image below:
This is good. Clicking 123 will show the number side.
The Question:
However, I want to by default show the number side and still be able to switch back to the letter side later WHEN THE KEYBOARD IS OPEN. How do I do this?
Setting the keyboard programmatically to a different type is NOT the answer!
For example: User clicks in the text field, keyboard pops up and defaults to let's say the ABC side. The user can click 123 to switch. This is what I want. If the ability to switch while the keyboard is open is taken away it defeats the point of this question.
So if the Name Phone Pad keyboard defaults to the ABC side initially. I want it to default to the 123 side that way while the keyboard is still open it can switch to ABC again when the user clicks ABC.
I want to do this because I have different settings for how to search for barcodes. Either by name of the product or number based on settings the user set about how to index results. This way if they are sorting by number it suggests numbers first, BUT they can still go back to searching by alphabetical if they wanted by clicking the ABC button on the keyboard.
Here is the current setting for my keyboard.
keyboardType is property for a UITextField.
textField.keyboardType = UIKeyboardType.UIKeyboardTypeNumberPad
and
textField.keyboardType = UIKeyboardType.UIKeyboardTypeDefault
is how you can switch between the two modes programatically. Hope that helps.
To switch the Keyboardlayout programmatically you have to mutate UITextfield's keyboardType attribute.
Try
textField.keyboardType = UIKeyboardType.NumberPad;
or
textField.keyboardType = UIKeyboardType.PhonePad;
Or maybe add a comma on the number pad without creating a custom keyboard.
I tried changing NSLocale to to another country but that didn't work. I have a function that separates multiple numbers from textFiled to evaluate them separately separated by commas but the keyboards do not contain a comma and i would rather not use the numbers and punctuation keyboard for this. Any help is appreciated. Using Xcode 7.2.
You can't change the keys on any of the built-in keyboards.
You can create your own keyboard view with the inputs you want (numbers, the comma, backspace) and set it as the inputView of your text field. The system will display your keyboard view instead of a standard keyboard when your text field becomes first responder.
Or you can just create, say, a toolbar with a comma button on it, and set the toolbar as your text field's inputAccessoryView, and let the text field use the standard decimal pad for numbers and backspace. The system will display your input accessory view above the standard keyboard when your text field becomes first responder.
Read “Input Views and Input Accessory Views” in the Text Programming Guide for iOS.
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.
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.
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.