Can I create a custom text box and keyboard for iOS without subclassing UIControl?

My application will require the user to enter chemistry formulas (H2O, Na ^ 2 +), and since indexes and superscripts can make text input confusing for the user, I decided to create a custom keyboard.

When a user enters a number as an index, it should be displayed as an index on the screen. The only way I found this is to use NSAttributedString, which apparently only works with CoreText. Since I cannot have a CoreText in a UITextField, I also need to create a custom text field. I looked in a UITextView, but the documentation says that font styles should be consistent throughout the text, i.e. I cannot have indexes.

Do I need to subclass UIControl for both cases and start from scratch? Or is there an alternative class that I can start with that already has some keyboard / text field properties?

+5
source share
1 answer

Yes, you need to create your own subclass UIView.

But you can easily support keyboard input by accepting a protocol UIKeyInput, redefining canBecomeFirstResponderit to return YESand add UITapGestureRecognizer:

    [self addGestureRecognizer:
     [[UITapGestureRecognizer alloc] initWithTarget:self
                                             action:@selector(becomeFirstResponder)]];

I want to allow the user to select ranges of text and use auto-correction, which is more complicated: you need to accept the protocol UITextInput.

Keyboard setup is very simple:

  • inputAccessoryView, , ,
  • inputView, , .
+5

All Articles