Class: UITextField
Overview
A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.
Instance Attribute Summary (collapse)
-
- adjustsFontSizeToFitWidth
A Boolean value indicating whether the font size should be reduced in order to fit the text string into the text field’s bounding rectangle.
-
- allowsEditingTextAttributes
A Boolean value indicating whether the user may edit the attributes of the text in the text field.
-
- attributedPlaceholder
The styled string that is displayed when there is no other text in the text field.
-
- attributedText
The styled text displayed by the text view.
-
- background
The image that represents the background appearance of the text field when it is enabled.
-
- borderStyle
The border style used by the text field.
-
- clearButtonMode
Controls when the standard clear button appears in the text field.
-
- clearsOnBeginEditing
A Boolean value indicating whether the text field removes old text when editing begins.
-
- clearsOnInsertion
A Boolean value indicating whether inserting text replaces the previous contents.
-
- delegate
The receiver’s delegate.
-
- disabledBackground
The image that represents the background appearance of the text field when it is disabled.
-
- editing
readonly
A Boolean value indicating whether the text field is currently in edit mode.
-
- font
The font of the text.
-
- inputAccessoryView
The custom accessory view to display when the text field becomes the first responder The default value of this property is nil.
-
- inputView
The custom input view to display when the text field becomes the first responder.
-
- leftView
The overlay view displayed on the left side of the text field.
-
- leftViewMode
Controls when the left overlay view appears in the text field.
-
- minimumFontSize
The size of the smallest permissible font with which to draw the text field’s text.
-
- placeholder
The string that is displayed when there is no other text in the text field.
-
- rightView
The overlay view displayed on the right side of the text field.
-
- rightViewMode
Controls when the right overlay view appears in the text field.
-
- text
The text displayed by the text field.
-
- textAlignment
The technique to use for aligning the text.
-
- textColor
The color of the text.
-
- typingAttributes
The attributes to apply to new text being entered by the user.
Attributes inherited from UIControl
#contentHorizontalAlignment, #contentVerticalAlignment, #enabled, #highlighted, #selected, #state, #touchInside, #tracking
Attributes inherited from UIView
#alpha, #autoresizesSubviews, #autoresizingMask, #backgroundColor, #bounds, #center, #clearsContextBeforeDrawing, #clipsToBounds, #contentMode, #contentScaleFactor, #exclusiveTouch, #frame, #gestureRecognizers, #hidden, #layer, #multipleTouchEnabled, #opaque, #restorationIdentifier, #subviews, #superview, #tag, #transform, #userInteractionEnabled, #window
Attributes inherited from UIResponder
Instance Method Summary (collapse)
-
- borderRectForBounds:
Returns the receiver’s border rectangle.
-
- clearButtonRectForBounds:
Returns the drawing rectangle for the built-in clear button.
-
- drawPlaceholderInRect:
Draws the receiver’s placeholder text in the specified rectangle.
-
- drawTextInRect:
Draws the receiver’s text in the specified rectangle.
-
- editingRectForBounds:
Returns the rectangle in which editable text can be displayed.
-
- leftViewRectForBounds:
Returns the drawing rectangle of the receiver’s left overlay view.
-
- placeholderRectForBounds:
Returns the drawing rectangle for the text field’s placeholder text You should not call this method directly.
-
- rightViewRectForBounds:
Returns the drawing location of the receiver’s right overlay view.
-
- textRectForBounds:
Returns the drawing rectangle for the text field’s text.
Methods inherited from UIControl
#actionsForTarget:forControlEvent:, #addTarget:action:forControlEvents:, #allControlEvents, #allTargets, #beginTrackingWithTouch:withEvent:, #cancelTrackingWithEvent:, #continueTrackingWithTouch:withEvent:, #endTrackingWithTouch:withEvent:, #removeTarget:action:forControlEvents:, #sendAction:to:forEvent:, #sendActionsForControlEvents:
Methods inherited from UIView
#addConstraint:, #addConstraints:, #addGestureRecognizer:, #addSubview:, #alignmentRectForFrame:, #alignmentRectInsets, animateWithDuration:animations:, animateWithDuration:animations:completion:, animateWithDuration:delay:options:animations:completion:, areAnimationsEnabled, beginAnimations:context:, #bringSubviewToFront:, commitAnimations, #constraints, #constraintsAffectingLayoutForAxis:, #contentCompressionResistancePriorityForAxis:, #contentHuggingPriorityForAxis:, #convertPoint:fromView:, #convertPoint:toView:, #convertRect:fromView:, #convertRect:toView:, #decodeRestorableStateWithCoder:, #didAddSubview:, #didMoveToSuperview, #didMoveToWindow, #drawRect:, #drawRect:forViewPrintFormatter:, #encodeRestorableStateWithCoder:, #endEditing:, #exchangeSubviewAtIndex:withSubviewAtIndex:, #exerciseAmbiguityInLayout, #frameForAlignmentRect:, #gestureRecognizerShouldBegin:, #hasAmbiguousLayout, #hitTest:withEvent:, #initWithFrame:, #insertSubview:aboveSubview:, #insertSubview:atIndex:, #insertSubview:belowSubview:, #intrinsicContentSize, #invalidateIntrinsicContentSize, #isDescendantOfView:, layerClass, #layoutIfNeeded, #layoutSubviews, #needsUpdateConstraints, #pointInside:withEvent:, #removeConstraint:, #removeConstraints:, #removeFromSuperview, #removeGestureRecognizer:, requiresConstraintBasedLayout, #sendSubviewToBack:, setAnimationBeginsFromCurrentState:, setAnimationCurve:, setAnimationDelay:, setAnimationDelegate:, setAnimationDidStopSelector:, setAnimationDuration:, setAnimationRepeatAutoreverses:, setAnimationRepeatCount:, setAnimationStartDate:, setAnimationTransition:forView:cache:, setAnimationWillStartSelector:, setAnimationsEnabled:, #setContentCompressionResistancePriority:forAxis:, #setContentHuggingPriority:forAxis:, #setNeedsDisplay, #setNeedsDisplayInRect:, #setNeedsLayout, #setNeedsUpdateConstraints, #setTranslatesAutoresizingMaskIntoConstraints:, #sizeThatFits:, #sizeToFit, #systemLayoutSizeFittingSize:, transitionFromView:toView:duration:options:completion:, transitionWithView:duration:options:animations:completion:, #translatesAutoresizingMaskIntoConstraints, #updateConstraints, #updateConstraintsIfNeeded, #viewForBaselineLayout, #viewPrintFormatter, #viewWithTag:, #willMoveToSuperview:, #willMoveToWindow:, #willRemoveSubview:
Methods inherited from UIResponder
#becomeFirstResponder, #canBecomeFirstResponder, #canPerformAction:withSender:, #canResignFirstResponder, #isFirstResponder, #motionBegan:withEvent:, #motionCancelled:withEvent:, #motionEnded:withEvent:, #nextResponder, #reloadInputViews, #remoteControlReceivedWithEvent:, #resignFirstResponder, #touchesBegan:withEvent:, #touchesCancelled:withEvent:, #touchesEnded:withEvent:, #touchesMoved:withEvent:
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Instance Attribute Details
- (Boolean) adjustsFontSizeToFitWidth
A Boolean value indicating whether the font size should be reduced in order to fit the text string into the text field’s bounding rectangle. Normally, the text field’s content is drawn with the font you specify in the font property. If this property is set to YES, however, and the contents in the text property exceed the text field’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached. The text is shrunk along the baseline. The default value for this property is NO. If you change it to YES, you should also set an appropriate minimum font size by modifying the minimumFontSize property.
- (Boolean) allowsEditingTextAttributes
A Boolean value indicating whether the user may edit the attributes of the text in the text field. If this property is set to YES, the user may edit the style information of the text. In addition, pasting styled text into the text field retains any embedded style information. If NO, the text field prohibits the editing of style information and strips style information from any pasted text. However, you can still set the style information programmatically using the methods of this class.The default value of this property is NO.
- (NSAttributedString) attributedPlaceholder
The styled string that is displayed when there is no other text in the text field. This property is nil by default. If set, the placeholder string is drawn using a 70% grey color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.
- (NSAttributedString) attributedText
The styled text displayed by the text view. This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.
- (UIImage) background
The image that represents the background appearance of the text field when it is enabled. When set, the image referred to by this property replaces the standard appearance controlled by the borderStyle property. Background images are drawn in the border rectangle portion of the image. Images you use for the text field’s background should be able to stretch to fit. This property is set to nil by default.
- (UITextBorderStyle) borderStyle
The border style used by the text field. The default value for this property is UITextBorderStyleNone. If the value is set to the UITextBorderStyleRoundedRect style, the custom background image associated with the text field is ignored.
- (UITextFieldViewMode) clearButtonMode
Controls when the standard clear button appears in the text field. The standard clear button is displayed at the right side of the text field as a way for the user to remove text quickly. This button appears automatically based on the value set for this property. The default value for this property is UITextFieldViewModeNever.
- (Boolean) clearsOnBeginEditing
A Boolean value indicating whether the text field removes old text when editing begins. If this property is set to YES, the text field’s previous text is cleared when the user selects the text field to begin editing. If NO, the text field places an insertion point at the place where the user tapped the field. Note: Even if this property is set to YES, the text field delegate can override this behavior by returning NO from its textFieldShouldClear: method.
- (Boolean) clearsOnInsertion
A Boolean value indicating whether inserting text replaces the previous contents. The default value of this property is NO. When the value of this property is YES and the text field is in editing mode, the selection UI is hidden and inserting new text clears the contents of the text field and sets the value of this property back to NO.
- (Object) delegate
The receiver’s delegate. A text field delegate responds to editing-related messages from the text field. You can use the delegate to respond to the text entered by the user and to some special commands, such as when the return button is pressed.
- (UIImage) disabledBackground
The image that represents the background appearance of the text field when it is disabled. Background images are drawn in the border rectangle portion of the image. Images you use for the text field’s background should be able to stretch to fit. This property is ignored if the background property is not also set. This property is set to nil by default.
- (Boolean) editing (readonly)
A Boolean value indicating whether the text field is currently in edit mode. (read-only) This property is set to YES when the user begins editing text in this text field, and it is set to NO again when editing ends. Notifications about when editing begins and ends are sent to the text field delegate.
- (UIFont) font
The font of the text. This property applies to the entire text of the text field. It also applies to the placeholder text. The default font is a 12-point Helvetica plain font. If you are using styled text in iOS 6 or later, assigning a new value to this property causes the font to be applied to the entirety of the string in the attributedText property. If you want to apply the font to only a portion of the text, create a new attributed string with the desired style information and associate it with the text field.
- (UIView) inputAccessoryView
The custom accessory view to display when the text field becomes the first responder The default value of this property is nil. Assigning a view to this property causes that view to be displayed above the standard system keyboard (or above the custom input view if one is provided) when the text field becomes the first responder. For example, you could use this property to attach a custom toolbar to the keyboard.
- (UIView) inputView
The custom input view to display when the text field becomes the first responder. If the value in this property is nil, the text field displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead.The default value of this property is nil.
- (UIView) leftView
The overlay view displayed on the left side of the text field. You can use the left overlay view to indicate the intended behavior of the text field. For example, you might display a magnifying glass in this location to indicate that the text field is a search field. The left overlay view is placed in the rectangle returned by the leftViewRectForBounds: method of the receiver. The image associated with this property should fit the given rectangle. If it does not fit, it is scaled to fit. If your overlay view does not overlap any other sibling views, it receives touch events like any other view. If you specify a control for your view, the control tracks and sends actions as usual. If an overlay view overlaps the clear button, however, the clear button always takes precedence in receiving events.
- (UITextFieldViewMode) leftViewMode
Controls when the left overlay view appears in the text field. The default value for this property is UITextFieldViewModeNever.
- (Float) minimumFontSize
The size of the smallest permissible font with which to draw the text field’s text. When drawing text that might not fit within the bounding rectangle of the text field, you can use this property to prevent the receiver from reducing the font size to the point where it is no longer legible. The default value for this property is 0.0. If you enable font adjustment for the text field, you should always increase this value. If you are using styled text in iOS 6 or later, assigning a new value to this property causes the minimum font size to be applied to the entirety of the string in the attributedText property. If you want to apply different minimum font sizes to different parts of your string, create a new attributed string with the desired style information and associate it with the text field.
- (String) placeholder
The string that is displayed when there is no other text in the text field. This value is nil by default. The placeholder string is drawn using a 70% grey color.
- (UIView) rightView
The overlay view displayed on the right side of the text field. You can use the right overlay view to provide indicate additional features available for the text field. For example, you might display a bookmarks button in this location to allow the user to select from a set of predefined items.The right overlay view is placed in the rectangle returned by the rightViewRectForBounds: method of the receiver. The image associated with this property should fit the given rectangle. If it does not fit, it is scaled to fit.If your overlay view does not overlap any other sibling views, it receives touch events like any other view. If you specify a control for your view, that control tracks and sends actions as usual. If an overlay view overlaps the clear button, however, the clear button always takes precedence in receiving events. By default, the right overlay view does overlap the clear button.
- (UITextFieldViewMode) rightViewMode
Controls when the right overlay view appears in the text field. The default value for this property is UITextFieldViewModeNever.
- (String) text
The text displayed by the text field. This string is nil by default.In iOS 6 and later, assigning a new value to this property also replaces the value of the attributedText property with the same text, albeit without any inherent style attributes. Instead the text view styles the new string using the font, textColor, and other style-related properties of the class.
- (NSTextAlignment) textAlignment
The technique to use for aligning the text. This property applies to the both the main text string and the placeholder string. The default value of this property is NSLeftTextAlignment. If you are using styled text in iOS 6 or later, assigning a new value to this property causes the text alignment to be applied to the entirety of the string in the attributedText property. If you want to apply the alignment to only a portion of the text, create a new attributed string with the desired style information and associate it with the text field.
- (UIColor) textColor
The color of the text. This property applies to the entire text string. The default value for this property is a black color. The value for the property can only be set to a non-nil value; setting this property to nil raises an exception.If you are using styled text in iOS 6 or later, assigning a new value to this property causes the text color to be applied to the entirety of the string in the attributedText property. If you want to apply the color to only a portion of the text, create a new attributed string with the desired style information and associate it with the text field.
- (Hash) typingAttributes
The attributes to apply to new text being entered by the user. This dictionary contains the attribute keys (and corresponding values) to apply to newly typed text. When the text field’s selection changes, the contents of the dictionary are cleared automatically.If the text field is not in editing mode, this property contains the value nil. Similarly, you cannot assign a value to this property unless the text field is currently in editing mode.
Instance Method Details
- (CGRect) borderRectForBounds(bounds)
Returns the receiver’s border rectangle. You should not call this method directly. If you want to provide a different border rectangle for drawing, you can override this method and return that rectangle. The default implementation of this method returns the original bounds rectangle.
- (CGRect) clearButtonRectForBounds(bounds)
Returns the drawing rectangle for the built-in clear button. You should not call this method directly. If you want to place the clear button in a different location, you can override this method and return the new rectangle. Your method should call the super implementation and modify the returned rectangle’s origin only. Changing the size of the clear button may cause unnecessary distortion of the button image.
- (Object) drawPlaceholderInRect(rect)
Draws the receiver’s placeholder text in the specified rectangle. You should not call this method directly. If you want to customize the drawing behavior for the placeholder text, you can override this method to do your drawing. By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or do the drawing yourself. If you do render the text yourself, you should not invoke super.
- (Object) drawTextInRect(rect)
Draws the receiver’s text in the specified rectangle. You should not call this method directly. If you want to customize the drawing behavior for the text, you can override this method to do your drawing. By the time this method is called, the current graphics context is already configured with the default environment and text color for drawing. In your overridden method, you can configure the current context further and then invoke super to do the actual drawing or you can do the drawing yourself. If you do render the text yourself, you should not invoke super.
- (CGRect) editingRectForBounds(bounds)
Returns the rectangle in which editable text can be displayed. You should not call this method directly. If you want to provide a different editing rectangle for the text, you can override this method and return that rectangle. By default, this method returns a region in the text field that is not occupied by any overlay views.
- (CGRect) leftViewRectForBounds(bounds)
Returns the drawing rectangle of the receiver’s left overlay view. You should not call this method directly. If you want to place the left overlay view in a different location, you can override this method and return the new rectangle.
- (CGRect) placeholderRectForBounds(bounds)
Returns the drawing rectangle for the text field’s placeholder text You should not call this method directly. If you want to customize the drawing rectangle for the placeholder text, you can override this method and return a different rectangle. If the placeholder string is empty or nil, this method is not called.
- (CGRect) rightViewRectForBounds(bounds)
Returns the drawing location of the receiver’s right overlay view. You should not call this method directly. If you want to place the right overlay view in a different location, you can override this method and return the new rectangle.
- (CGRect) textRectForBounds(bounds)
Returns the drawing rectangle for the text field’s text. You should not call this method directly. If you want to customize the drawing rectangle for the text, you can override this method and return a different rectangle. The default implementation of this method returns a rectangle that is derived from the control’s original bounds, but which does not include the area occupied by the receiver’s border or overlay views.