UIKeyboardTypeNamePhonePad - Display options - ios

I need to use the, Name Phone Keyboard type (UIKeyboardTypeNamePhonePad) but having the number pad appear first when the text field is selected.
I need to input data such as
123 -
657 -
450b -
123a
I always need input a number first, but occasionally either a A / B at the end.

You can try to change keyboard type in function of the size of the string:
Implement delegate UITextFieldDelegate
Then set defaut textfield keyboard;
[textField setKeyboardType:UIKeyboardTypeNumberPad];
[textField setDelegate:self];
Then in the following method, change the keyboard when text size equals 3:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([[textField text] length] > 3)
{
[textField setKeyboardType:UIKeyboardTypeDefault];
[textField reloadInputViews];
}
return YES;
}
Another solution is to add an accessory view button (you can change of keyboard manually):
- (void)viewDidLoad
{
[super viewDidLoad];
textField.keyboardType = UIKeyboardTypeNumberPad;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 50, 50)];
[button addTarget:self action:#selector(changeKeyboard) forControlEvents:UIControlEventTouchDown];
textField.inputAccessoryView = button;
}
-(void)changeKeyboard
{
if([textField keyboardType] == UIKeyboardTypeNumberPad)
{
textField.keyboardType = UIKeyboardAppearanceDefault;
}
else
{
textField.keyboardType = UIKeyboardTypeNumberPad;
}
[textField reloadInputViews];
}

click on the textfield,then go to Hide or show utilities ie View(you will find it at right hand side beside the editor.There you select the third view ie the most right one).
Then you click the attributes inspector.In that you will find a menu called "keyboard" which is set as default,but you can select whichever pad you want.
Hope it works for you.

Related

"Next" button custom keyboard Objective-C

I've created a custom keyboard using Objective-C. However, I'm stuck on a certain feature and that is being able to implement a "Next" button like that already available on the stock iOS keyboard. (I've attached a screen shot of the stock iOS keyboard below).
Here is my keyboard view controller code that connects my button voids to the actual keyboard :
-(void)addGesturesToKeyboard{
[self.keyboard.globeSwitch addTarget:self action:#selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.planA addTarget:self action:#selector(execPlanA) forControlEvents:UIControlEventTouchUpInside];
// what I want to implement -> [self.keyboard.planA addTarget:self action:#selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.planB addTarget:self action:#selector(execPlanB) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.planC addTarget:self action:#selector(execPlanC) forControlEvents:UIControlEventTouchUpInside];
}
And here is my textFieldshouldReturn method that doesn't seem to be working with me :
-(void)textFieldShouldReturn:(UITextField*)textField
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
}

How to add DatePicker dynamically under UITextField

I'm not sure it can be able to add DatePicker dynamically under UITextField when tapped on. If possible, help me to show sample code for that.
on click event set on text field
- (void)viewDidLoad {
[super viewDidLoad];
[textField addTarget:self action:#selector(clicked:) forControlEvents:UIControlEventEditingDidBegin];
}
set text field edition false
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
Method when clickd on text filed
-(IBAction)clicked:(id)sender
{
[sender resignFirstResponder];
picker1 = [[UIDatePicker alloc] initWithFrame:CGRectMake(textField.frame.origin.x-40, textField.frame.origin.y+textField.frame.size.height, textField.frame.size.width, 200)];
[picker1 setDatePickerMode:UIDatePickerModeDate];
picker1.backgroundColor = [UIColor whiteColor];
[picker1 addTarget:self action:#selector(startDateSelected:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:picker1];
}
when date value changed this method is called
-(IBAction)startDateSelected:(id)sender
{
NSLog(#"%#",picker1.date);
[textField setText:[NSString stringWithFormat:#"%#",picker1.date
]];
}
I hope it will help you! :)

How can i change action of the button "return" on the keyboard on Xcode?

i need to change the text of the button "return" on my keyboard,and i even need to change its action.
The action should be a new paragraph.
Can you help me?
You cannot rename the Return button to any custom text
Some default values thatThe return key can take are
typedef enum : NSInteger {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
} UIReturnKeyType;
where Default is "return" others are Go, Google, Yahoo as is.
look here.
And for capturing the return Event you can use the textView Delegate method
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
NSLog(#"Pressed Return Key");
} else {
NSLog(#"Pressed Any Other Key");
}
return YES;
}
UITextField has this delegate method that you can implement, and get's called when you press return on the keyboard:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
You can add a new line char "\n" to the textView text and it would go to next line.
UITextView it's suppose to work like that where it would go to a new line when you press Return.
click and set your keyword here
[textField setReturnKeyType:UIReturnKeyDone];
for keyboard implement the protocol in your class, set
textfield.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// write you code here what you want .
return YES;
}
refer this if you want to UITextView How to dismiss keyboard for UITextView with return key?
Here is the way to add custom button over the keyboard. But apple could reject your app so be careful.
- (void)viewWillAppear:(BOOL)animtated {
// Register the observer for the keyboardWillShow event
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animtated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification {
// create custom buttom
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(self.view.frame.size.width- 100, self.view.frame.size.height - 200.0f, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton addTarget:self action:#selector(addParaGraph:) forControlEvents:UIControlEventTouchUpInside];
[doneButton setBackgroundColor:[UIColor redColor]];
[doneButton setTitle:#"Add Paragraph" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView *keyboard;
for (int i = 0; i < [tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
//if ([[keyboard description] hasPrefix:#"<UIPeripheralHostView"] == YES) {
[keyboard addSubview:doneButton];
//}
}
}
- (void)addParaGraph:(id)sender{
// Write here you code to add new paragraph
}
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
// Set the FirstResponder of the UITextField on the layout
[theTextField resignFirstResponder];
return YES;
}
Swift 3
let addressTextField = UITextField()
addressTextField.placeholder = "Search or enter website name"
addressTextField.layer.borderColor = UIColor.gray.cgColor
addressTextField.layer.borderWidth = 1.0
addressTextField.returnKeyType = .go
view.addSubview(addressTextField)
addressTextField.delegate = self
extension YourNaneController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}

How do I centre UITextField cursor after user input consistently

I have a UITextField that I want to centre all content (text, cursor) at all times. Is this possible in iOS 7? My current initialisation of the view is shown below.
self.textField = [[UITextField alloc] init];
self.textField.delegate = self;
[self.textField setTextAlignment:NSTextAlignmentCenter];
self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
self.textField.placeholder = NSLocalizedString(#"Enter some text", #"The placeholder text to use for this input field");
My requirement for this is that when I click in the UITextField, the placeholder text should disappear, and show the cursor in the middle of the UITextField.
Currently, this seems to be intermittently positioned either in the middle of the text field or on the left of the text field irrelevant of where I click. Anybody got any suggestions as to how I can solve this or is it a known issue in iOS?
If you're creating you UITextField in you Storyboard you should not initialise and alloc it in code.
You need to use you Textfield delegates to accomplish this..
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
[self.textField setTextAlignment:NSTextAlignmentCenter];
self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
self.textField.placeholder = #"Enter some text";
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
//this removes your placeholder when textField get tapped
self.textField.placeholder = nil;
//this sets your cursor to the middle
self.textField.text = #" ";
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
self.textField.placeholder = #"The placeholder text to use for this input field";
}
This surely does the trick.. Please accept the answer if it helps.
UPDATE
With code below when user press backspace the cursor will not align the text to the left.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *proposedNewString = [[textField text] stringByReplacingCharactersInRange:range withString:string];
NSLog(#"propose: %#", proposedNewString);
if ([proposedNewString isEqualToString:#""])
{
textField.text = [#" " stringByAppendingString:textField.text];
}
return YES;
}

how to pop up datePicker when text filed click and disappear when editing done

i have view something like below
date picker view is always there how can i make it appear pop up when Enter date is clicked and when i click on back ground then date picker should go down
i have just made date picker view customary but i dont know how to do this appear and disappear thing
Ok. Here is some sample code for your requirement with animation.
- (void) showView
{
[self.view addSubview:yourDatePickerView];
yourDatePickerView.frame = CGRectMake(0, -250, 320, 50);
[UIView animateWithDuration:1.0
animations:^{
yourDatePickerView.frame = CGRectMake(0, 152, 320, 260);
}];
}
And here is how to hide your DatePickerView
- (void) hideView
{
[UIView animateWithDuration:0.5
animations:^{
yourDatePickerView.frame = CGRectMake(0, -250, 320, 50);
} completion:^(BOOL finished) {
[yourDatePickerView removeFromSuperview];
}];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == yourDateTextField)
{
[self showView];
return NO; // preventing keyboard from showing
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField == yourDateTextField)
{
[self hideView];
}
}
That's all you need.
You should identify the textfield in the delegate method.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == DATE_TEXT_FIELD)
{
//Display date picker
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField == DATE_TEXT_FIELD)
{
//Hide date picker
}
}
Create one UIView reference which contains this date picker view.
Now assign this datePickerContainerView to textField.inputView
property.
Then assign textfield.delegate to self and implement your
textFieldShouldReturn method. In that method, write this lines
[textField resignFirstResponder];
return YES;
Now when you tap on that textfield, it will load datepicker view in place of default keyboard, as per inputview property settings.
Before doing the code for datePicker,make sure that you type the following line of code
[textField resignFirstResponder];
As we know,keyboard is the default first responder of a textField.So you should resign it first and then code for picker otherwise the keyboard will pop up as well.

Resources