Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my custom keyboard which I believe addresses these as completely as Apple will allow:</p> <pre><code>// PVKeyboard.h #import &lt;UIKit/UIKit.h&gt; @interface PVKeyboard : UIView @property (nonatomic,assign) UITextField *textField; @end // PVKeyboard.m #import "PVKeyboard.h" @interface PVKeyboard () { UITextField *_textField; } @property (nonatomic,assign) id&lt;UITextInput&gt; delegate; @end @implementation PVKeyboard - (id&lt;UITextInput&gt;) delegate { return _textField; } - (UITextField *)textField { return _textField; } - (void)setTextField:(UITextField *)tf { _textField = tf; _textField.inputView = self; } - (IBAction)dataPress:(UIButton *)btn { [self.delegate insertText:btn.titleLabel.text]; } - (IBAction)backPress { if ([self.delegate conformsToProtocol:@protocol(UITextInput)]) { [self.delegate deleteBackward]; } else { int nLen = [_textField.text length]; if (nLen) _textField.text = [_textField.text substringToIndex:nLen-1]; } } - (IBAction)enterPress { [_textField.delegate textFieldShouldReturn:_textField]; } - (UIView *)loadWithNIB { NSArray *aNib = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; UIView *view = [aNib objectAtIndex:0]; [self addSubview:view]; return view; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) [self loadWithNIB]; return self; } @end </code></pre> <p>In XCode 4.3 and later, you need to create an objective-Class (for the .h &amp; .m files) based on UIView and a User Interface View file (for the .xib file). Make sure all three files have the same name. Using the Identity Inspector, make sure to set the XIB's File's Owner Custom Class to match the new object's name. Using the Attributes Inspector, set the form's size to Freeform and set the Status Bar to none. Using the Size Inspector, set the form's size, which should match the width of the standard keyboard (320 for iPhone portrait and 480 for iPhone landscape), but you can choose any height you like.</p> <p>The form is ready to be used. Add buttons and connect them to the dataPress, backPress and enterPress as appropriate. The initWithFrame: and loadWithNIB functions will do all the magic to allow you to use a keyboard designed in Interface Builder.</p> <p>To use this keyboard with a UITextField myTextField, just add the following code to your viewDidLoad:</p> <pre><code>self.keyboard = [[PVKeyboard alloc]initWithFrame:CGRectMake(0,488,320,60)]; self.keyboard.textField = self.myTextField; </code></pre> <p>Because of some limitations, this keyboard isn't reusable, so you'll need one per field. I can almost make it reusable, but I'm just not feeling that clever. The keyboard is also limited to UITextFields, but that's mainly because of limitations in implementing the enter key functionality, which I'll explain below.</p> <p>Here's the magic that should allow you to design a better keyboard than this starter framework...</p> <p>I've implemented the only property of this keyboard, textField, using a discreet a discrete setter (setTextField) because:</p> <ol> <li>we need the UITextField object to handle the enter problem</li> <li>we need UITextField because it conforms to the UITextInput protocol which conforms to UIKeyInput, which does much of our heavy lifting</li> <li>it was a convenient place to set the UITextInput's inputView field to use this keyboard.</li> </ol> <p>You'll notice a second private property named delegate, which essentially typecasts the UITextField pointer to a UITextInput pointer. I probably could have done this cast inline, but I sensed this might be useful as a function for future expansion, perhaps to include support for UITextView.</p> <p>The function dataPress is what inserts text input the edited field using the insertText method of UIKeyInput. This seems to work in all versions back to iOS 4. For my keyboard, I'm simply using the label of each button, which is pretty normal. Use whatever NSStrings strike your fancy.</p> <p>The function dataBack does the backspace and is a little more complicated. When the UIKeyInput deleteBackward works, it works wonderfully. And while the documentation says it works back to iOS 3.2, it seems to only work back to iOS 5.0, which is when UITextField (and UITextView) conformed to the UITextInput protocol. So prior to that, you're on your own. Since iOS 4 support is a concern to many, I've implemented a lame backspace which works on the UITextField directly. If not for this requirement, I could have made this keyboard work with UITextView. And this backspace isn't as general, only deleting the last character, while deleteBackward will work properly even if the user moves the cursor.</p> <p>The function enterPress implements the enter key, but is a complete kludge because Apple doesn't seem to give a method for invoking the enter key. So enterPress simply calls the UITextField's delegate function textFieldShouldReturn:, which most programmers implement. Please note that the delegate here is the UITextFieldDelegate for the UITextField and NOT the delegate property for the keyboard itself.</p> <p>This solution goes around the normal keyboard processing, which hardly matters in the case of UITextField, but makes this technique unusable with UITextView since there is now way to insert line breaks in the text being edited.</p> <p>That's pretty much it. It took 24 hours of reading and cobbling to make this work. I hope it helps somebody.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload