enablesReturnKeyAutomatically is not working with numeric keypad - ios

I am implementing a message composer as like iMessages.
enablesReturnKeyAutomatically of UITextView is not working.
I have set it as textView.enablesReturnKeyAutomatically = YES;
but when I tap on numeric keypad and start typing something and sent using my send button. the textView got empty but the send button (return button of UITextView) is still enabled.

enablesReturnKeyAutomatically seems to only affect the uikeyboard, which in this case is a numeric keypad. The default numeric keypad does not have a return key, so there is nothing to enable/disable.
It sounds like you have a separate send button in the view. A workaround is inside - textView:shouldChangeTextInRange:replacementText: calculate what the new string will be with the replacementText and test if this new String is equal to #"". If so, then sendButton.enabled = NO, else sendButton.enabled = YES.
Remember to set the UITextViewDelegate and to trim the new string of white space.

Related

Setting capitalization of the first letter on keyboard

I have a button which presents an alert with a text field in it. When clicked on the button it automatically focuses in the text field and keyboard shows. However the letters in the keyboard start as lower-case. How can I fix that so it starts as upper-case?
Edit:
alertTextField.autocapitalizationType = .sentences
This was what I was looking for.
Try this
let mytextfield = UITextField()
mytextfield.autocapitalizationType = .allCharacters
Other types :

UITextView receiving text form dictation, but text is nil in textView.text property

I am trying to get the text from a UITextView whose entry method was dictation. In the textViewDidChange delegate method, I have tried printing the following info:
print(textView.text)
print(myTextView.text)
print(textView.hasText)
print(textView.attributedText)
print(textView.textStorage)
all of these come back nil, except the hasText comes back as false.
The text is visible right there in the textView, but it is not being registered. I should say that when I go edit another form in the field, and then try to get the value from this text field, then the the text entered IS visible in the textView's textView.text property. But it's like it takes a few moments of editing other fields to fully "register" with the textView object.
Any idea what could be happening here?
Try this after a while
let when = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: when) {
print(textView.text)
print(textView.hasText)
print(textView.hasText)
print(textView.attributedText)
print(textView.textStorage)
}
Update: It`s because you are trying to print the text before setting it. It takes a little time to set your text into textView.

UITextView autocapitalization is not working when keyboard is active

Currently, I am setting autocapitalization in a button target like this
//This method is fired when keyboard is still active
- (IBAction)changeAutoCapitalization:(id)sender
{
self.textView.autocapitalizationType = UITextAutocapitalizationTypeWords;
}
But this won't make keyboard to capital.
You have to call [self.textView reloadInputViews] after modifying the UITextAutocapitalizationType in order to change keyboard immediately.
If you find that you're doing a lot of text-related customizations, it might be helpful to subclass UITextView, to keep the functionality encapsulated.
Note: If testing on the simulator, make sure you are using the
Simulator's keyboard, by going to the menu: I/O -> Keyboard -> Toggle
Software Keyboard
If you are adding text automatically, for instance in response to a button tap, and the shift key turns off, then you can use this pattern:
autocapitalizationType = .allCharacters //Activate shift key
autocorrectionType = .no //Don't show all-caps auto suggest
reloadInputViews() //Apply changes
Then, to restore to sentences capitalization, for instance when a line is cleared:
if (autocapitalizationType = .allcharacters) {
autocapitalizationType = .sentences //Activate sentence capitalization
autocorrectionType = .yes //Show auto suggest
reloadInputViews() //Apply changes
}
Other capitalization options:
.none
.words
You may also need to do this when any non-enter key is pressed. To do so, override insertText from UIKeyInput, and check the entered text string:
if (text != "\n") {
if (autocapitalizationType == .allCharacters) {
autocapitalizationType = .sentences //Activate sentence capitalization
autocorrectionType = .yes //Show auto suggest
reloadInputViews() //Apply changes
}
//Respond to any character sequences
//...
}
Try to add this after initialization:
self.textView.autocapitalizationType = UITextAutocapitalizationTypeWords;
OR
It's very likely that the "Auto-Capitalization" option is turned off on that device. You can find that option in Settings > General > Keyboard.

Append text to a UITextField with custom keyboard made out of buttons

I have a static numeric-keyboard made out of a bunch of buttons, I also have three UITextFields, textField1, textField2 and textField3 where I'm inputting the text using the static keyboard.
Here is the code I'm using to detect which textField is currently in focus and to input the content of the buttons. It kind of works but I don't like the fact that I have three IF statements and I'm not sure how to prevent the keyboard from appearing when a textField is tapped.
What would be the best way to implement this functionality?
#IBAction func appendKey(sender: AnyObject) {
let digit = sender.currentTitle!
if(textField1.isFirstResponder()){
textField1.text = textField1.text! + digit!
}else if(textField2.isFirstResponder()){
textField2.text = textField2.text! + digit!
}else if(textField3.isFirstResponder()){
textField3.text = textField3.text! + digit!
}
}
Thanks
If the standard keyboard is displaying then your custom keyboard isn't setup properly. Your custom keyboard should be the inputView of each UITextField. If you do that, the standard keyboard won't appear and yours will instead.
Your custom keyboard should be a separate class that handles all of it's own buttons. It appears you have everything in one view controller - all of the text fields, all of the buttons, and all of the button handling code. This is a bad approach. Create your custom keyboard class view. Put all of the code to handle and display the buttons in that custom view class. Create a single instance of this view in your view controller and assign the custom keyboard view instance to the inputView property of each text field.
In the custom keyboard class, listen for the UITextFieldTextDidBeginEditingNotification notification. This is how you keep track of the current text field. Your custom keyboard class should not have any specific reference to any text field other than track the current one. It should also ensure that the text field's inputView is itself.
In each button handler of the custom keyboard class, get the text you wish to append and then call the text field's insertText: method with the string. That's it. This will ensure the text is inserted and/or replaced based on the current selecting in the text field.

dynamically change UIKeyboards return key

I have two UITextfield the user enters his name into the first and email into the second. I would like to know how to change the UIKeyboards return key depending if the name text field has an entry or not.
For instance if nametextfield is empty then I would like the UIkeyboard return key to be Next
else if the nametextfield has an entry in it then when the user selects the email text field I would like the return key to be submit.
Is this possible? if so how would I go about accomplishing it? any help would be appreciated.
You can have return key customized to prefixed values that you can see in UIReturnKeyType enum for each UITextField.
textFieldName.returnKeyType = UIReturnKeyNext;
textFieldEmail.returnKeyType = UIReturnKeyDefault;
Not sure if this is what you're looking for though.
You have a chance to set up keyboard characteristics in the UITextFieldDelegate Protocol method textFieldShouldBeginEditing: which is called before the text field becomes the first responder (indeed to decide if it may become the first responder). If you don't already have a delegate for the text field(s) in question you would have to assign one and implement at least that method. Presumably the same object handling the text field could hold the delegate methods. The following implementation sets the return key to "Search".
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"textFieldShouldBeginEditing");
textField.returnKeyType = UIReturnKeySearch;
return YES;
}
You'd have to look at the contents of your text fields to decide what value to use.
Make use of the textField.returnKeyType property.
you can check out all the available options here http://developer.apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/doc/c_ref/UIReturnKeyType
textfield.returnKeyType = UIReturnKeySearch;

Resources