viernes, 13 de agosto de 2010

iPhone's Numeric Pad keyboard with a DOT

I was developing an application for iPhone which handl
es numeric fields to register information. To my disappointment, even in iOS 4, there is no default keyboard for a Number pad including a dot “.”; there is a Number pad with numbers from 0 to 9 and the backspace, in the other hand we have the Numbers and punctuations, which includes numbers, dot, parenthesis, and other symbols. T
he problem with the former is the absence of a dot and the later is that we can easily switch to the alphabetical keyboard.

But what happen with the Number pad including a dot? The Number pad keyboard includes a blank space


After a little search a found a couple of examples of including the dot in the kwyboard, using the blank space.

First, lets say we have a text field defined and connected to the interface

IBOutlet UITextField * textMoney;

And wich keyboard type is the Number Pad

self.textMoney.keyboardType = UIKeyboardTypeNumberPad;

In order to include the Dot, we need to stat receiving notifications to act propperly when the keyboard shows:

/**

View will appear

**/

- (void)viewWillAppear:(BOOL)animtated

{

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)

{

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

}

else

{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

}

}


We include the notification, according with the detected iOS.

/**

View will disappear

**/

- (void)viewWillDisappear:(BOOL)animtated

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}


The dismiss te notification once the view is not showed.

Now, all interesting process begin with when we received the notification in the configured function, in this case keyboardWillShow

/**

The Keyboard will be shown

**/

- (void)keyboardWillShow:(NSNotification *)notification

{

if ([textNotes isFirstResponder])

{

// Skip for Notes keyboard

return;

}

// Locate fist view

UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

UIView *keyboard;

// Define Button

UIButton * utilityButton = [UIButton buttonWithType:UIButtonTypeCustom];

utilityButton.frame = CGRectMake(0, 163, 105, 53);


// Set Text and font

[utilityButton.titleLabel setFont:[UIFont systemFontOfSize:35]];

[utilityButton setTitle:[Dot or Comma according with localized settings] forState:UIControlStateNormal];

// Set Solors

[utilityButton setTitleColor:[UIColor colorWithRed:77.0f/255.0f green:84.0f/255.0f blue:98.0f/255.0f alpha:1.0] forState:UIControlStateNormal];

[utilityButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

// Background for the pressed state

[utilityButton setBackgroundImage:[UIImage imageNamed:@"background.png"] forState:UIControlStateHighlighted];

// Add call to functionality

[utilityButton addTarget:self action:@selector(addDecimalPointToField) forControlEvents:UIControlEventTouchUpInside];

// Behavior when orientation is changed

[utilityButton setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight)];


// Locate keyboard view and add button

for(int i=0; i<[tempWindow.subviews count]; i++)

{

keyboard = [tempWindow.subviews objectAtIndex:i];

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)

{

if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)

{

[keyboard addSubview:utilityButton];

}

}

else

{

if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)

{

[keyboard addSubview:utilityButton];

}

}

}

}


Again, we need to make a distinction for the iOS.

One little extra t
hing, to add the dot to the current field with the focus, we use a trick, copy the dot to memory and then call the paste function of the current field, this is done in addDecimalPointToField.

/**

Send Decimal Point event to field with focus

**/

- (void)addDecimalPointToField

{

UIView * objectToEdit = nil;

// Locate field with the focus

for (UIView * localView in self.scrollView.subviews)

{

if ([localView isFirstResponder])

{

objectToEdit = localView;

}

}

// Try to insert dot

if (objectToEdit != nil)

{

// Detect if the Dot has been already inserted

NSString * localText = [(UITextField *)objectToEdit text];

NSRange separatorPosition = [localText rangeOfString:[Dot or Comma according with localized settings]];


if (separatorPosition.location == NSNotFound)

{

// Dot wasn't found, add it

[objectToEdit insertText:[NSString stringWithFormat:[Dot or Comma according with localized settings]]];

}

}

}


I have change part of the code with [According with the localization options, add a Dot or a Comma], this in order to let you decide what punctuation symbol to use.

The result will be something like:


I'm not really sure it this will be accepted by Apple to be release to iTunes Store, I haven't use it for a commercial application, at least, my AdHoc application implements it.

Hope this help you.

No hay comentarios: