How to change text of return key and make a Custom UIReturnKeyType - ios

We have a number of types available for keyboard return buttons in swift 4 like done, yahoo etc. I want the return key to be Add how can I do that? Is there a way to define a custom UIReturnKeyType type?

There is no way to customize UIReturnKeyType in terms of behavior, button text etc.
For your case, .go or .send might be appropriate.
You can also put a UIButton above the keyboard with the text of your choice.

Related

Custom iOS keyboard - Custom font output

I am trying to create a custom keyboard using the app keyboard extension. I am happy with the layout but the output is depended on the UITextField's font.
Is there a way to force a different font (use special characters?) while using the keyboard ?
Thank you
It depends.
Text field (or any other view that draws text) uses 2 informations on how to show some text. One is the sequence of characters called String and the other one is how the string should be represented. The second one is then split it things like fonts, colors, line height, line breaking and wrapping...
So the keyboard alone is not enough to for instance present a certain part of word using different fonts. You need at least a bit of access to the item that represents the text. So if you have no access to your text field then the answer is; No, you can not fore a different font when using different keyboard.
If you do have the access then the answer should lie in NSAttributedString. It is a string you can assign to most items under attributedText. This class wraps your raw string and can add many properties to parts of text you want to change. That includes using a different font.
Another approach would be using HTML tags. Again you will need to process this using for instance NSAttributedString or display it with another element like web view.
I would try it with using NSAttributedString. Hook up to delegate and implement textField(: shouldChangeCharactersIn: replacementString:. The implementation itself may still not be easy though.

iOS, textView half editable

I need help in string formatting in iOS UITextView. I have a textView, for now it`s editable. Now I want to add a few strings. they should be like header and user must be able to type only under it, and when he press ENTER or DELETE line sting should change their position, so at the end it should look like
Header 1(with color not editable)
/* user can type here
*/
Header 2 (with color not editable)
/* user can type here
*/
I think i need to use delegate and attributed string or something,
but I can`t figure it out. HELP please
The best way to achieve this is to make two labels and two text views on top of a scroll view. like
The heights of two text views can by dynamic depending on the text length. And in this case you can avoid endless IF conditions as you would need to put everything in a single Text view.
The advantage of this approach is the label text format will be very easy to set, you can simply set the attributed string in the storyboard.

Dynamically change keyboard's return button but preserve type

I have an input field the user needs to fill with an alphanumeric code. The keyboard the user uses to type the code has a dynamic return button that changes to "send" as he writes some text on the field. When the field is empty the return button has the default value.
To dynamically change the return button type I use the following code:
if([textField.text isEqualToString:#""])
{
textField.returnKeyType = UIReturnKeyDefault;
}
else
{
textField.returnKeyType = UIReturnKeySend;
}
[textField reloadInputViews];
However this has the following drawback: since the code is alphanumeric, the user may be typing numbers, and yet the keyboard will always switch back to the letter keyboard, so to type more than one number in a row he will need to be continuously switching to number keyboard.
Is there any way to dynamically change the return key of a keyboard as the user types but to preserve the keyboard state to letters or numbers keyboard?
I think this is not a bug on Apple's side, more a missing implementation of API for the keyboard. With the new iOS8 API you might want to create your own keyboard returning the UIKeyboardType.
For iOS7 I worked around by inspecting the views of the keyboard. Use the US2KeyboardType CocoaPod or the source:
https://github.com/ustwo/US2KeyboardType
As Martin noted above, it's not a bug on Apple's side, but on my side. However I'll be posting the solution I've found since it is the one that solves that particular problem:
Instead of manually changing the return key type when there is text on the text field, Apple provides us with a property called enablesReturnKeyAutomatically that when set to YES it automatically manages the issue, enabling and disabling the return key depending on whether there is text or not in the text field.
Therefore you don't need to modify the returnKeyType property, and thus, no calling to reloadInputViews is required, so the keyboard doesn't change back to its original state.

Code input text field

Is there a native UI control for code input text field, for example like Whatsapp:
No. To achieve this, they're almost certainly tapping into the textField:shouldChangeCharactersInRange:replacementString: method for their UITextField, selectively accepting and formatting user input to match the dash-if-empty approach.
Further, I'm sure they've subclassed the field; per your comments there isn't a blue cursor - which isn't standard for a UITextField.
No there isn't. Use a UITextField, fill it with dashes, keep track of how many characters the user has entered, and replace the dashes accordingly as the user types.
There's a 4-digit code input text field called CodeInputView written in Swift.
In the past I've added a UITextField to the view and set its hidden == true. Then I show/hide the keyboard by calling becomeFirstResponder()/resignFirstResponder() on it. I listen for text did change notifications and update a visible label with the value of the hidden text field.

changing keyboard return key label text

Iphone's onscreen keyboard has a number of predefined labels for its return key: done, return, go, google, etc. In my case I just want it to say Save, but it's not in the list. How can I create my own?
You can't. UIKeyboard is read-only.

Resources