keyBoard of textfield in iPhone- using alphanumeric chararacter - iphone

I am new to iPhone technology and I want to use TextField, but when I use keyboard I can't use alphanumeric character. It's working only for alphabetic words.
Some functions I have used are below:-
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if(theTextField == txtanswer) {
[txtanswer resignFirstResponder];
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
//[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
//[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(cehc==2)
{
}if(textField == txtanswer)
[txtanswer resignFirstResponder];
return YES;
}
-(IBAction) textFieldDoneEditing : (id) sender{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
Please help me out to solve this problem
Thanks
Kunal

Does UIKeyboardType in the XCode docs help you (or did I misunderstand your problem)? Note that there seems to be 3 different numeric keyboard types.

Related

Multiple UITextFields with UIPicker and UIKeyboard

So I have about 7 textfields and 6 of them use a keyboard and the other uses a picker. The problem I'm having is that if the keyboard is open when the textfield that is linked to the picker is touched the keyboard won't dismiss and the picker appears under it. Here's my code
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
pickerView.hidden = YES;
if ([textField isEqual:state])
{
[state resignFirstResponder];
[self textFieldFirstResponderOnDelay1];
}
else
{
pickerView.hidden = YES;
// This movie the view up so textfield isn't hidden by keyboard
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
-(void) viewDidLoad
{
pickerView = [[UIPickerView alloc] init];
pickerView.frame = CGRectMake(0, 245, 320, 216);
pickerView.delegate = self;
pickerView.hidden = YES;
pickerView.showsSelectionIndicator = YES;
state.inputView = pickerView;
[self.view addSubview:pickerView];
}
-(void)textFieldFirstResponderOnDelay1
{
pickerView.hidden=NO;
[pickerView reloadAllComponents];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if([textField isEqual:state])
{
}
else
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
Instead of adding the pickerview as a subview of your main window, just set it as the input view for the appropriate textviews, then it will show/hide as the keyboard would normally:
textField.inputView = pickerView;

scrolling on view highlighting?

I am trying to scroll through textarea fields in the view using a next button on top of the keyboard, when it shifts to the last textarea field it's scrolling downwards and hiding behind the keyboard.
code:
(CGRect)getObjectRectFromRoot {
int yOffset = 0;
yOffset += self.view.frame.origin.y;
UIView* suView = self.view.superview;
while (suView) {
yOffset += suView.frame.origin.y;
suView = suView.superview;
}
CGRect r;
r.size.width = 700;
r.size.height = 700;
r.size.width = self.view.frame.size.width;
r.size.height = self.view.frame.size.height;
r.origin.x = 0;
r.origin.y = yOffset;
return r;
}
Implementing this code in the two delegate methods of your textField will do what you want. Here is the textFieldDidBeginEditing:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
And here is the textFieldDidEndEditing:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[self textFieldShouldReturn:textField];
[UIView commitAnimations];
}
If you want it for the buttons you can just setup an IBAction with the above code.
The code can also be used with any other type of action or view you would like!

show textfield above keyboard for all interface orientations?

Actually In my iPhone app i used the following code to lift the view in order to show the textfield above the keyboard. My application support all four interface orientations. I use the following code to lift the view. Problem here is that it only works for portrait orientation. Is there any way to implement the correct lifting in all four orientations
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UIInterfaceOrientation orientation = [Globals getUIInterfaceOrientation];//[[UIApplication sharedApplication] statusBarOrientation];
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0) {
heightFraction = 0.0;
}
else if (heightFraction > 1.0) {
heightFraction = 1.0;
}
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
else
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
You can try setting the textField’s inputAccessoryView property to itsself. I haven't tried this, but in theory it should make the textField attach itself to the keyboard and thus always be displayed:
textField.inputAccessoryView = textField;

How to Drag my view a little bit up when keyboard comes up?

In my app i have some textfields which gets filled through the keyboard. There are 6 of them and the last two are in very bottom of the screen and it gets hidden whenever keyboard comes up.
Any suggestions??
Make the textFields delegate as self and add the following code to the textFieldDidBeginEditing and textFieldDidEndEditing.
Define the following constants: (the constants have been set for iPad. Tweak them for iphone)
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 302;
Add two variables in the .h file of your class. CGFloat animatedDistance; and UITextField *currentField;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentField = textField;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
CGRect textFieldRect =[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
if (orientation == UIInterfaceOrientationLandscapeLeft )
{
textFieldRect.origin.y = textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationLandscapeRight )
{
textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y;
}
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
animatedDistance = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
currentField = nil;
}
Use a UIScrollview so the user can scroll down. You can make use of the inputAccessoryView of a UITextField to show buttons like next and previous, on click let the input jump to the next textfield and scroll to it.
Have a look at this question and answers at SO, same problem, different solution.

how UITextField should come up in UIScrollView?

I have a UIScrollView, added as
[self.view addSubview:myScrollView];
There are 8 to 10 UITextFields, so when I click textFields for writing something, they Keyboard comes on them, and they became back to keyboard, now what should I do so that they will look after keyboard end ???
If someone is not clear, may ask again ....
This is how your .h file should look like
#interface LoginScreen : UIViewController <UITextFieldDelegate>
{
...
UITextField *textView;
CGFloat animatedDistance;
}
//put this in your viewcontroller.m file
-(void)textFieldDidBeginEditing:(UITextField *)textField{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGRect textFieldRect =
[self.view convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)txtObject {
[txtObject resignFirstResponder];
return YES;
}
Pls go through these
How to make a UITextField move up when keyboard is present?
Keyboard in the way of textfields how to move view up
iPhone Keyboard Covers UITextField
How programmatically move a UIScrollView to focus in a control above keyboard?