Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a custom text field and keyboard for iOS w/o subclassing UIControl?

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?

like image 805
Mahir Avatar asked Dec 22 '11 06:12

Mahir


1 Answers

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:

  • either, you override inputAccessoryView which allow you to display a custom view attached to the keyboard,
  • either, you override inputView so it returns a custom view that will be displayed in place of the keyboard.
like image 150
Nicolas Bachschmidt Avatar answered Oct 16 '22 22:10

Nicolas Bachschmidt