Class: UILabel
Overview
The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. The base UILabel class provides support for both simple and complex styling of the label text. You can also control over aspects of appearance, such as whether the label uses a shadow or draws with a highlight. If needed, you can customize the appearance of your text further by subclassing.
Instance Attribute Summary (collapse)
-
- adjustsFontSizeToFitWidth
A Boolean value indicating whether the font size should be reduced in order to fit the title string into the label’s bounding rectangle.
-
- adjustsLetterSpacingToFitWidth
A Boolean value indicating whether spacing between letters should be adjusted to fit the string within the label’s bounds rectangle.
-
- attributedText
The styled text displayed by the label.
-
- baselineAdjustment
Controls how text baselines are adjusted when text needs to shrink to fit in the label.
-
- enabled
The enabled state to use when drawing the label’s text.
-
- font
The font of the text.
-
- highlighted
A Boolean value indicating whether the receiver should be drawn with a highlight.
-
- highlightedTextColor
The highlight color applied to the label’s text.
-
- lineBreakMode
The technique to use for wrapping and truncating the label’s text.
-
- minimumScaleFactor
The minimum scale factor supported for the label’s text.
-
- numberOfLines
The maximum number of lines to use for rendering text.
-
- preferredMaxLayoutWidth
The preferred maximum width (in points) for a multiline label.
-
- shadowColor
The shadow color of the text.
-
- shadowOffset
The shadow offset (measured in points) for the text.
-
- text
The text displayed by the label.
-
- textAlignment
The technique to use for aligning the text.
-
- textColor
The color of the text.
-
- userInteractionEnabled
A Boolean value that determines whether user events are ignored and removed from the event queue.
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, #window
Attributes inherited from UIResponder
#inputAccessoryView, #inputView, #undoManager
Instance Method Summary (collapse)
-
- drawTextInRect:
Draws the receiver’s text (or its shadow) in the specified rectangle.
-
- textRectForBounds:limitedToNumberOfLines:
Returns the drawing rectangle for the label’s text.
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 title string into the label’s bounding rectangle. Normally, the label text is drawn with the font you specify in the font property. If this property is set to YES, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached. This property is effective only when the numberOfLines property is set to 1.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.Important: If this property is set to YES, it is a programmer error to set the lineBreakMode property to a value that causes text to wrap to another line.
- (Boolean) adjustsLetterSpacingToFitWidth
A Boolean value indicating whether spacing between letters should be adjusted to fit the string within the label’s bounds rectangle. When this property is YES, the label may alter the letter spacing of the label text to make that text fit better within the label’s bounds. This property is applied to the string regardless of the current line break mode. The default value of this property is NO. If the value of this property to YES, the label to ignore values value returned by the tighteningFactorForTruncation method of any NSParagraphStyle objects associated with the label text. Important: If this property is set to YES, it is a programmer error to set the lineBreakMode property to a value that causes text to wrap to another line.
- (NSAttributedString) attributedText
The styled text displayed by the label. 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.
- (UIBaselineAdjustment) baselineAdjustment
Controls how text baselines are adjusted when text needs to shrink to fit in the label. If the adjustsFontSizeToFitWidth property is set to YES, this property controls the behavior of the text baselines in situations where adjustment of the font size is required. The default value of this property is UIBaselineAdjustmentAlignBaselines. This property is effective only when the numberOfLines property is set to 1.
- (Boolean) enabled
The enabled state to use when drawing the label’s text. This property determines only how the label is drawn. Disabled text is dimmed somewhat to indicate it is not active. This property is set to YES by default.
- (UIFont) font
The font of the text. 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 label. If you are not using styled text, this property applies to the entire text string in the text property.The default value for this property is the system font at a size of 17 points (using the systemFontOfSize: class method of UIFont). The value for the property can only be set to a non-nil value; setting this property to nil raises an exception.
- (Boolean) highlighted
A Boolean value indicating whether the receiver should be drawn with a highlight. Setting this property causes the receiver to redraw with the appropriate highlight state. A subclass implementing a text button might set this property to YES when the user presses the button and set it to NO at other times. In order for the highlight to be drawn, the highlightedTextColor property must contain a non-nil value.The default value of this property is NO.
- (UIColor) highlightedTextColor
The highlight color applied to the label’s text. Subclasses that use labels to implement a type of text button can use the value in this property when drawing the pressed state for the button. This color is applied to the label automatically whenever the highlighted property is set to YES. The default value of this property is nil .
- (NSLineBreakMode) lineBreakMode
The technique to use for wrapping and truncating the label’s text. If you are using styled text in iOS 6 or later, assigning a new value to this property causes the line break mode to be applied to the entirety of the string in the attributedText property. If you want to apply the line break mode to only a portion of the text, create a new attributed string with the desired style information and associate it with the label. If you are not using styled text, this property applies to the entire text string in the text property.This property is in effect both during normal drawing and in cases where the font size must be reduced to fit the label’s text in its bounding box. This property is set to NSLineBreakByTruncatingTail by default. Important: If this property is set to a value that causes text to wrap to another line, it is a programmer error to set either the adjustsFontSizeToFitWidth or adjustsLetterSpacingToFitWidth property to YES.
- (Float) minimumScaleFactor
The minimum scale factor supported for the label’s text. Use this property to specify the smallest multiplier for the current font size that yields an acceptable font size to use when displaying the label’s text. If you specify a value of 0 for this property, the current font size is used as the smallest font size.The default value of this property is 0.
- (Integer) numberOfLines
The maximum number of lines to use for rendering text. This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0. If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode. When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.
- (Float) preferredMaxLayoutWidth
The preferred maximum width (in points) for a multiline label. This property affects the size of the label when layout constraints are applied to it. During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label.
- (UIColor) shadowColor
The shadow color of the text. The default value for this property is nil, which indicates that no shadow is drawn. In addition to this property, you may also want to change the default shadow offset by modifying the shadowOffset property. Text shadows are drawn with the specified offset and color and no blurring.
- (CGSize) shadowOffset
The shadow offset (measured in points) for the text. The shadow color must be non-nil for this property to have any effect. The default offset size is (0, -1), which indicates a shadow one point above the text. Text shadows are drawn with the specified offset and color and no blurring.
- (String) text
The text displayed by the label. 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 label styles the new string using the shadowColor, textAlignment, and other style-related properties of the class.
- (NSTextAlignment) textAlignment
The technique to use for aligning the text. 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 label. If you are not using styled text, this property applies to the entire text string in the text property.The default value of this property is NSTextAlignmentLeft.
- (UIColor) textColor
The color of the text. If you are using styled text in iOS 6 or later, assigning a new value to this property causes the 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 label. If you are not using styled text, this property applies to the entire text string in the text property.The default value for this property is a black color (set through the blackColor class method of UIColor). The value for the property can only be set to a non-nil value; setting this property to nil raises an exception.
- (Boolean) userInteractionEnabled
A Boolean value that determines whether user events are ignored and removed from the event queue. This property is inherited from the UIView parent class. This class changes the default value of this property to NO.
Instance Method Details
- (Object) drawTextInRect(rect)
Draws the receiver’s text (or its shadow) in the specified rectangle. You should not call this method directly. This method should only be overridden by subclasses that want to modify the default drawing behavior for the label’s text. 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) textRectForBounds(bounds, limitedToNumberOfLines:numberOfLines)
Returns the drawing rectangle for the label’s text. You should not call this method directly. This method should only be overridden by subclasses that want to change the receiver’s bounding rectangle before performing any other computations. Use the value in the numberOfLines parameter to limit the height of the returned rectangle to the specified number of lines of text. For this method to be called, there must be a prior call to the sizeToFit or sizeThatFits: method. Note that labels in UITableViewCell objects are sized based on the cell dimensions, and not a requested size.The default implementation of this method returns the original bounds rectangle.