Numeric keyboard with decimal separator - iphone

How can I get the numeric keyboard with the decimal separator? Currently I am stuck with UIKeyboardNumberPad.

you can try this
UIKeyboardTypeDecimalPad

typedef enum {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeDecimalPad,
UIKeyboardTypeTwitter,
UIKeyboardTypeWebSearch,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable // Deprecated
} UIKeyboardType;
If you don't like those, you can always modify them in code...
http://openradar.appspot.com/6635276
This has a solution:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html

The UIKeyboardTypeNumbersAndPunctuation is probably the least-bad option, unless you have the time and resources to roll your own keyboard.

The iPhone SDK currently does not offer a keypad (0-9) + decimal button UIKeyboard. I suspect this was designed because for the majority of use cases, it is not needed.
See this previous StackOverflow question discussing the (lack of) need for a decimal separator for currency inputs. I also wrote a blog post about this very topic.
However, if you really do need a keypad + decimal button keyboard, your only option will be to roll your own custom keyboard. Contrary sbwoodside's answer, you really want to shy away from customizing the existing UIKeyboard. While this may be easy to do, it relies upon private classes that can change anytime...meaning your hack could break in the future without warning.

Related

iPhone: UITextfield validation

I checked the existing examples here in S.O, for my case which one do you think is better,
There are many textfields in one view and I want to validate them just when the user is editing that particular textfield, cause otherwise I have to validate them together when user press next on the screen than I have show all validation messages which I dont want to bother with. I want to validate the textfield in 2 different ways, if its a number input I want to validate it in a defined range e.g if number between between 5 and 1000 or if its a text then if the lenght of the text is in a defined range e.g between 2 and 10 characters. And the user must be able to enter any input out of range.
Which is better;
Using textField:shouldChangeCharactersInRange
or something like this:
[textField addTarget:self action:#selector(validateField:) forControlEvents:UIControlEventEditingChanged];
And either the case how can I check on runtime that the number entered is in range and dont allow user to enter bigger or smaller numbers
Good one.
The answer for your question is
1) Set the tag values to all the textfields.
2) Use UIControlEventEditingDidBegin and trigger a method to validate the textfield before the currentone.
[textfield1 setTag:1];
[textfield2 setTag:2];// make sure to set tag values incrementally
[textfield2 addTarget:self #selector(validate:) forControlEvents:UIControlEventEditingDidBegin];
-(void) validate:(id) sender
{
// 10 is the number of textfields, change it accordingly.
if(sender.tag!=1 && sender.tag!=10)
{
//validate textfield with tag sender.tag-1
}
else
{
//handle first and last textfield here
}
}
Try this, probably this will solve your problem.
Actually, both of your approaches will be just fine! Using the delegate of the UITextField to call textField:shouldChangeCharactersInRange and perform the validation there is an equally good approach as using Target & Action. In both situations, you define a handler that receives the calling UITextField as an argument (one implementation of this is what #Anil Kumar suggested in his answer to tag the text fields. but this is actually not even required, you can just call isEqual: on the text fields directly to check for equality).
Personally, I tend to use delegation for these kinds of tasks though.
Update: If you want to go really fancy on this problem, you'll use Reactive Cocoa. The April Tech Talk of the Ray Wenderlich Gang had a great session on it and also discusses the issue of from validation, so it'll just match your case! :)
Take a look at TextFieldValidator developed in swift

How to add Decimal Separator character to iOS keyboard Number Pad?

How can I create a number pad with a decimal point separator character option in Xcode's Interface Builder Storyboard?
Changing Keyboard Type to Number Pad doesn't show a decimal option:
In iOS 4.1, there is a number pad with a decimal point available. You can select it programatically (in case you are supporting versions of iOS before 4.1) like so:
inputBoxTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
if (([[[UIDevice currentDevice] systemVersion] doubleValue] >= 4.1)) {
inputBoxTextField.keyboardType = UIKeyboardTypeDecimalPad;
}
You could choose any appropriate keyboard type (line 1). In this case, NumbersAndPunctuation includes a period in case the user's iOS version is before 4.1.
In Swift:
priceText.keyboardType = .decimalPad
In iOS 8 or above follow these steps:
Select the Textfield in Interface Builder
Edit Keyboard Type in xib or Storyboard
Select Decimal Pad
I have developed an open source custom decimal keyboard. Methods and implementations are very easy to use and you can change buttons or add/remove extra buttons as you see fit. Please have at look at the repository here TD-DecimalKeyboard and feel free to fork and contribute.

how prevent changing keyboard numbers to alphabets by pressing ABC key

I place some textfields in my app,those textfield allows only numbers 0,1..9 and .symbol.
For that my code is
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
static NSCharacterSet *charSet = nil;
if(!charSet) {
charSet = [[[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet] retain];
}
NSRange location = [string rangeOfCharacterFromSet:charSet];
return (location.location == NSNotFound);
}
It works fine,
But when ever i press ABC & #+= buttons keypad changes to another type.
I need to prevent that,user can use numbers and .,X keys only.
Rest of them not active ,if they are visible that not the problem
How can i done this.
can any one ple help me.
Thank u in advance.
There is a keyboard designed for this: UIKeyboardTypeDecimalPad
If you can target your app for iOS 4.1 and later, use #jtbandes suggestion.
The main advantage of using built-in keyboard types in iPhone SDK is they support internationalization. If you don't need letters, just numbers, I would suggest just building a custom keyboard from UIButtons with just the keys you want. Keep the keyboard from displaying (resignFirstResponder on textFieldShouldBeginEditing). You can include playing "Tock.aiff" from the "com.apple.UIKit" bundle to make it seem authentic.
If you need to support OS versions prior to 4.1, you can do what App Cubby did in some of their apps (like Gas Cubby) - they used the number pad keyboard (UIKeyboardTypeNumberPad) and overlaid a UIButton containing the dot in the bottom left corner. The keyboard handles the 0-9 input and the action for dot button is configured to append a '.' to the current string value.

Format Integer as Two Places Decimal

I am presenting the user with a field that displays the number keyboard and only want them to be able to enter numbers--no decimals. Since the field is a currency field, however, I want the number to always display with two decimal places. You've probably seen this type of an interface at an ATM.
I've looked at the NSNumberFormatter docs and general formatting using [NSString stringWithFormat], but it's not clear to me a good way to do this. I am listening on the field with a selector for UIControlEventEditingChanged events. It's in that selector I want to do the formatting. When I use [NSString stringWithFormat: #"%.2f", amount], it always places amount to the left of the decimal. I want it to always be the fraction portion until the third digit is entered, so if the user types 32, the actual amount is .32. If the user enters 173, the value is actually 1.73. If the user enters 10047, the actual value should be 100.47 etc.
Any suggestions?
Thanks.
Treat the amount they're entering as cents instead of dollars. Divide it by 100 before formatting.

What's the UITableView index magnifying glass character?

In Apple's iPhone apps (like Contacts), they have a nice magnifying glass icon at the top of the table view index. Since the table view index API is character-based, I assume that this magnifying glass is a Unicode character. So far I've resorted to placing a question mark character there, but that looks lame.
Can anyone tell me what character the magnifying glass is?
Returning UITableViewIndexSearch as section index title (same as #"{search}") also works.
In Swift you would use UITableView.indexSearch.
#"{search}" FTW.
Return that title and it's magically replaced by the correct glyph.
In tableView:sectionForSectionIndexTitle:AtIndex: explicitly scroll to the top and return NSNotFound:
- (NSInteger) tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index {
if (index == 0) {
[tableView setContentOffset:CGPointZero animated:NO];
return NSNotFound;
}
return index;
}
There already is an existing UTF codepoint for the magnifying glass. It is U+1F50D. However it's slightly tricky getting Xcode to recognize this number. According to Apple's String Programming Guide the UTF bits should be split into two UTF-16 surrogate pairs (0xD83D, 0xDD0D). Check with this surrogate pair calculator for reference.
An NSString instance with the contents of the surrogate pair can be obtained with:
[NSString stringWithFormat:#"%C%C", 0xD83D, 0xDD0D];
A constant UITableViewIndexSearch is used in the case, as noted here:
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Section_Index_Icons
You can certainly put a Unicode character in the table view index (I've done this with other characters) and put your header in the first table section in lieu of of the tableViewHeader. That said, I've yet to find the magnifying glass in any of the unicode references. The closes I've found is the Telephone Recorder symbol - ⌕ (\u2315). Unfortunately, it points in the wrong direction and the "handle" extends into the "magnifying glass."
In the sectionIndexTitlesForTableView add a NSMutableArray* titles for example and in addition to your indexes add the [titles addObject: UITableViewIndexSearch]
I know this is an old post but since it's similarly related to what I was looking for... If anyone is looking for the Unicode Character of the magnifying glass, it would be this one \u128269 represented by this: 🔍 that is if your browser is displaying unicode
You can view the full detail of it on this web site:
http://www.charbase.com/1f50d-unicode-left-pointing-magnifying-glass
In my case I wanted to do a search input box with the magnifying glass in it, I manage to do it via this piece of code, please note that we need to use a special font that might not work in older browser or OS:
<input type="text" style="font-family: Segoe UI Symbol;" placeholder="🔍">
This works on my machine Win7 in both Firefox and Chrome but IE is always an exception with placeholder in this case.
Someone claims that Apple told them this isn't supported in the SDK.
I can understand why someone might not want to use the image, although it is very pretty... characters are so much easier to maintain and can scale with little effort.
⚦ is another unicode option for a magnifying lens-like glyph.. again it's not facing the correct direction.. I believe it's really some kind of hermaphroditic gender symbol. The html for the unicode symbol above is ⚦
I really think a magnifying lens symbol should be added to the unicode character set under "26: Misc. Symbols".
You could use a space as the character for the index and then overlay an UIImageView that has user interaction disabled and contains the following image:
(source: booleanmagic.com)