UITextField
and
UITextFieldDelegate
let us input one line of text.
UITextView
(in
this example)
and
UITextViewDelegate
let us input and display many lines of text.
See
Text
Fields
in the HIG,
Managing
Text Fields,
and
key
clicks.
Our
View
contains a
UITextField
,
which is a control object that has one delegate object.
For simplicity,
the
View
acts as the
UITextField
’s
delegate.
In addition to the delegate,
the text field may have zero or more targets.
For simplicity,
our text field has no targets.
The
UITextField
has already adopted the
UITextInputTraits
protocol,
which includes properties such as
keyboardType
and
autoCapitalizationType
.
We will have to make the delegate adopt the
UITextFieldDelegate
protocol.
That means we have to write
textFieldShouldReturn:
textFieldDidEndEditing:
View.m
.
The text field font defaults to
systemFontOfSize:
17 points.
(One inch equals 72 points.)
initWithFrame:
The keyboard is 216 pairs of pixels high (432 pixels) on a 4-inch diagonal retina iPhone in portrait orientation. This fact is nowhere in Apple’s documentation, and may thus change without warning. See Moving Content that is Located Under the Keyboard. In iOS 7, the keyboad is translucent. That’s why it’s bluish in the above screenshot.
main.m
PigAppDelegate
View
UITextBorderStyleNone
,
UITextBorderStyleBezel
,
UITextBorderStyleRoundedRect
.
×
in the circle.
Try a different clear button mode:
UITextFieldViewModeNever
(the clear button is never visible),
UITextFieldViewModeWhileEditing
(the clear button is visible only while editing),
etc.
UIKeyBoardTypeDefault
(the default),
UIKeyboardTypePhonePad
,
UIKeyboardTypeEmailAddress
,
etc.
UIReturnKeyDefault
,
UIReturnKeyGoogle
,
etc.
UITextAutocapitalizationTypeNone
,
UITextAutocapitalizationTypeWords
,
UITextAutocapitalizationTypeSentences
,
UITextAutocapitalizationTypeAllCharacters
.
UITextAutocorrectionTypeYes
, etc.
@"watermelon"
.
Even better, reject any string that contains
@"watermelon"
.
See
rangeOfString:
.
textFieldShouldReturn:
returns
YES
or
NO
?
What difference does it make?
It makes no difference to
UIControlEventEditingDidEndOnExit
or to
UITextFieldTextDidEndEditingNotification
.
UIKeyboardTypeNumberPad
.
Use an
NSRegularExpression
to accept
only a line that consists of exactly nine digits and no other characters.
- (BOOL) textFieldShouldReturn: (UITextField *) tf { if ([tf.text isEqualToString: @""]) { textField.placeholder = @""; } else { NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: @"^\\d{9}$" options: 0 error: &error]; if (regex == nil) { NSLog(@"didn't create regex: %@", error); } else { NSUInteger numberOfMatches = [regex numberOfMatchesInString: tf.text options: 0 range: NSMakeRange(0, [tf.text length])]; if (numberOfMatches == 1) { [tf resignFirstResponder]; //Hide keyboard. } } } return YES; }
/* This method is called after the user enters (or deletes) each character. It inserts a dash after the third and fifth digit of the social security number. */ - (BOOL) textField: (UITextField *) tf shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString *) string { //Insert (or delete) the character that the user just entered. tf.text = [tf.text stringByReplacingCharactersInRange: range withString: string]; //If the text field now holds 3 or 6 characters, append a dash. if (tf.text.length == 3 || tf.text.length == 6) { tf.text = [tf.text stringByAppendingString: @"-"]; } //The character that the user entered has already been inserted, //so there's no need to insert it again. return NO; }