UIKeyboardTypeDecimalPad with negative numbers

I'm working on an iOS app that requires the user to enter numbers into a UITextField using the UIKeyboardTypeDecimalPad keyboard type. However, I just realized that there is no support for entering negative numbers, which is an application requirement.

Any ideas or thoughts on how I can go about doing this?


source to share

5 answers

This is clearly not the best answer. But I cannot delete it since it is accepted.

can this code help you:

if you want a negative number just use "-"

NSString *fieldString = [NSString stringWithFormat:@"%@",Textfield.text];


        int fieldValue;
        value = [fString intValue];


this will work for decimal numbers

            double fieldValue;
            value = [fString doubleValue];




You can use the UIToolbar as a kind of auxiliary input for your UITextField and place a button with a "+/-" (plus / minus) sign.

UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *plusMinusBbi = [[UIBarButtonItem alloc]initWithTitle:@"+/-" style:UIBarButtonItemStylePlain target:self action:@selector(togglePositiveNegative:)];
toolbar.items = @[plusMinusBbi];
self.textField.inputAccessoryView = toolbar;




I do not believe that something like this is possible right now (since Apple has not yet implemented it). The only option is to create your own keyboard or use the full ASCII file.



From what I have found, Apple has not yet implemented such a standard keyboard. However, you can add UIButtons to any keyboard window. this link should help or a similar tutorial This link should also help

Basically you register an NSNotificationListener to listen to the keyboard. Take the keyboard frame and add a UIButton to its view. The above link is not exactly what we want, but its the correct idea.

In appDelegate,

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

    - (void)keyboardDidShow:(NSNotification *)note
// Get the Very Top Window on the Display. That where the Keyboard is.
NSInteger topWindow = [[[UIApplication sharedApplication] windows] count] - 1;
UIWindow *keyboard = [[[UIApplication sharedApplication] windows] objectAtIndex:topWindow];
// If the dot has not been created (first time the keyboard has been displayed) create it.
if (self.dot == nil)
    self.dot = [UIButton buttonWithType:UIButtonTypeCustom];
    // Make the dot a subview of the view containing the keyboard.
    [keyboard addSubview:self.dot];
    // Place the dot in the correct location on the keyboard.
    [self.dot setFrame:CGRectMake(0, 427, 106, 53)];
    // Set the overlay graphics. (Use TransDecimalDown.png and TransDecimalUp.png for the Alert Style Keyboard.
    [self.dot setImage:[UIImage imageNamed:@"DecimalUp.png"] forState:UIControlStateNormal];
    [self.dot setImage:[UIImage imageNamed:@"DecimalDown.png"] forState:UIControlStateHighlighted];
    // Give the dot something to do when pressed.
    [self.dot addTarget:self action:@selector(sendDecimal:)  forControlEvents:UIControlEventTouchUpInside];
// Bring the dot to the front of the keyboard.
[keyboard bringSubviewToFront:self.dot];

    - (void)sendDecimal:(id)sender {
// Post a notification that the dot has been pressed. Observing view controllers are then responsible for adding the actual decimal.
[[NSNotificationCenter defaultCenter] postNotificationName:@"DecimalPressed" object:nil];
// Play the Keyboard Click. If the user has these sound effects turned off, the decimal will still click. Sorry.  :(  (Also, doesn't seem to work on the simulator, no keyboard clicks seem to.)


sorry for the awful code format, but you get the idea :)



To accomplish the task I came up using a .inputAccessoryView with a textbox

In View Mode DidLoad

self.textField.inputAccessoryView = [self accessoryViewForTextField:self.textField];



- (UIView *)accessoryViewForTextField:(UITextField *)textField{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
view.backgroundColor = [UIColor lightGrayColor];

UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[minusButton setTitle:@"-" forState:UIControlStateNormal];
[doneButton setTitle:NSLocalizedString(@"Done", @"Done") forState:UIControlStateNormal];
minusButton.backgroundColor = [UIColor magentaColor];
doneButton.backgroundColor = [UIColor blueColor];
CGFloat buttonWidth = view.frame.size.width/3;
minusButton.frame = CGRectMake(0, 0, buttonWidth, 44);
doneButton.frame = CGRectMake(view.frame.size.width - buttonWidth, 0, buttonWidth, 44);

[minusButton addTarget:self action:@selector(minusTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[doneButton addTarget:self action:@selector(doneTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];

[view addSubview:minusButton];
[view addSubview:doneButton];

return view;



this will add the custom view just above the keyboard as part of it

finally to get a minus

#pragma mark - IBActions

- (IBAction)minusTouchUpInside:(id)sender
NSString *value = self.textField.text;
if (value.length > 0) {
    NSString *firstCharacter = [value substringToIndex:1];
    if ([firstCharacter isEqualToString:@"-"]){
        self.textField.text = [value substringFromIndex:1];
        self.textField.text = [NSString stringWithFormat:@"-%@", value];

- (IBAction)doneTouchUpInside:(id)sender
    [self.textField resignFirstResponder];




All Articles