My app will require the user to input chemistry formulas (H2O, Na^2+), and since subscripts and superscripts may make the typing process confusing for the user, I have decided to make a custom keyboard.
When the user types in a number as a subscript, it should be displayed as a subscript on the screen. The only way I've found to do this is using NSAttributedString, which apparently only works with CoreText. Since I can't have CoreText in a UITextField, I need to create a custom text field as well. I was looking into UITextView, but the documentation reads that the font styles must be consistent throughout the text, meaning I can't have subscripts.
Do I need to subclass UIControl for both cases and start from scratch? Or is there an alternative class I can start with that already has some of the properties of keyboard/textfield?
Yes, you need to create your own subclass of UIView
.
But you can easily support keyboard input by adopting the UIKeyInput
protocol, overriding canBecomeFirstResponder
so it returns YES
and adding a UITapGestureRecognizer
:
[self addGestureRecognizer:
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(becomeFirstResponder)]];
I you want to allow the user to select ranges of text and use autocorrection, that's harder: you need to adopt the protocol UITextInput
.
Customizing the keyboard is really simple:
inputAccessoryView
which allow you to display a custom view attached to the keyboard,inputView
so it returns a custom view that will be displayed in place of the keyboard.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With