iPhone's Numeric Pad keyboard with a DOT
IBOutlet UITextField * textMoney;
self.textMoney.keyboardType = UIKeyboardTypeNumberPad;
/**
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];
}
}
/**
View will disappear
**/
- (void)viewWillDisappear:(BOOL)animtated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/**
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];
}
}
}
}
/**
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]]];
}
}
}
Comments