天天看点

iOS开发之-Text EditingText Views

Text Views

Managing Text Fields and Text Views

The Sequence of Messages to the Delegate

  1. Just before a text object becomes first responder—textFieldShouldBeginEditing: (text field) and textViewShouldBeginEditing: (text view).The delegate can verify whether the text object should become first responder by returningYES (the default) orNO.
  2. Just after a text object becomes first responder—textFieldDidBeginEditing: (text field) and textViewDidBeginEditing: (text view).
  3. During the editing session—The delegate of a text view can receive atextViewDidChange: message when any text changes. The delegate of a text field can receive atextFieldShouldClear: message when the user taps the clear button of a text field; the delegate returns a Boolean value indicating whether the text should be cleared.
  4. Just before a text object resigns first responder—textFieldShouldEndEditing: (text field) and textViewShouldEndEditing: (text view).The primary reason for a delegate to implement these methods is to validate entered text. The default return value isYES.

    A related method for text fields is textFieldShouldReturn:. When the user taps the return key, the text field class sends atextFieldShouldReturn: message to the delegate to ask whether it should resign first responder.

  5. Just after text a object resigns first responder—textFieldDidEndEditing: (text field) and textViewDidEndEditing: (text view).A delegate can implement these methods to get the text that the user has just entered or edited. 

Objects other than the delegate can be informed of changes in the first-responder status of text views and text fields by observingnotifications. The notifications have names such asUITextFieldTextDidBeginEditingNotification, UITextViewTextDidEndEditingNotification, and UITextViewTextDidChangeNotification. As with textFieldDidEndEditing: and textViewDidEndEditing:, the primary reason for observing and handling theUITextFieldTextDidEndEditingNotification and UITextViewTextDidEndEditingNotification notifications is to access the text in the associated text field or text view.

Configuring Text Fields and Text Views

Some properties are common to text views and text fields, and others are specific to each type of object, including the following:

  • Text characteristics—Text color, alignment, font family, font typeface, and font size.
  • Keyboard—Keyboard type, return key name, secure text entry, and auto-enabled return key, all of which are declared by theUITextInputTraits protocol.
  • Text-field specific—Border, background image, disabled image, clear button, and placeholder text. As aUIControl object, text fields also have highlighted, selected, enabled, and other properties.
  • Text-view specific—Editable status, data detectors (for phone numbers and URL links). Because a text view inherits fromUIScrollView, you can also manage scroll-view behavior by setting the appropriate properties.

Tracking Multiple Text Fields or Text Views

If the currently displayed view has multiple text fields or text views, the delegate must find a way to identify the text object that is the subject of a delegation message.

You can make this determination using one of two approaches: outlets or tags. 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField == self.someTextField) {
            // .....
            return NO;
        }
    return YES;
}
           

Defining outlet connections for the text objects in a view is especially useful, even essential, when you need to write string values to these objects, not just obtain them.

enum {
    NameFieldTag = 0,
    EmailFieldTag,
    DOBFieldTag,
    SSNFieldTag
};
           

Then assign the integer value to the tag property of the text object, either programmatically or in the attribute inspector of Interface Builder. (Thetag property is declared by UIView.) 

- (void)textFieldDidEndEditing:(UITextField *)textField {
 
    switch (textField.tag) {
        case NameFieldTag:
            // do something with this text field
            break;
        case EmailFieldTag:
             // do something with this text field
            break;
        // remainder of switch statement....
    }
}
           

Getting the Entered Text and Setting Text

The best delegation methods for accessing entered text are textFieldDidEndEditing: (text fields) and textViewDidEndEditing: (text views).

Listing 2-3  Getting the text entered into a text field
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text isEqualToString:@""])
        return;
 
    switch (textField.tag) {
        case NameFieldTag:
            [thePerson setObject:textField.text forKey:MyAppPersonNameKey];
            break;
        case EmailFieldTag:
            [thePerson setObject:textField.text forKey:MyAppPersonEmailKey];
            break;
        case SSNFieldTag:
            [thePerson setObject:textField.text forKey:MyAppPersonSSNKey];
            break;
        default:
            break;
    }
}
           
Listing 2-4  Getting the text entered into a text view
- (void)textViewDidEndEditing:(UITextView *)textView {
    NSString *theText = textView.text;
    if (![theText isEqualToString:@""]) {
        [thePerson setObject:theText forKey:MyAppPersonNotesKey];
    }
    doneButton.enabled = NO;
}
           

Tracking the Selection in Text Views

The textViewDidChangeSelection: method of UITextViewDelegate lets you track changes to the selections that a user makes in a text view. You can implement the method to obtain the selected substring and do something with it. 

Listing 2-12  Getting the selected substring and changing it
- (void)textViewDidChangeSelection:(UITextView *)textView {
    NSRange r = textView.selectedRange;
    if (r.length == 0) {
        return;
    }
    NSString *selText = [textView.text substringWithRange:r];
    NSString *upString = [selText uppercaseString];
    NSString *newString = [textView.text stringByReplacingCharactersInRange:r withString:upString];
    textView.text = newString;
}