| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/UIKit.framework |
| Availability | Available in iPhone OS 2.0 and later. |
| Declared in | UILabel.h |
| Related sample code |
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 control over the appearance of your text, including whether it uses a shadow or draws with a highlight. If needed, you can customize the appearance of your text further by subclassing.
The default content mode of the UILabel class is UIViewContentModeRedraw. This mode causes the view to redraw its contents every time its bounding rectangle changes. You can change this mode by modifying the inherited contentMode property of the class.
New label objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UILabel, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
text property
font property
textColor property
textAlignment property
lineBreakMode property
enabled property
adjustsFontSizeToFitWidth property
baselineAdjustment property
minimumFontSize property
numberOfLines property
highlightedTextColor property
highlighted property
shadowColor property
shadowOffset property
userInteractionEnabled property
For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.
A Boolean value indicating whether the font size should be reduced in order to fit the title string into the label’s bounding rectangle.
@property(nonatomic) BOOL adjustsFontSizeToFitWidth
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.
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.
UILabel.hControls how text baselines are adjusted when text needs to shrink to fit in the label.
@property(nonatomic) UIBaselineAdjustment baselineAdjustment
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.
UILabel.hThe enabled state to use when drawing the label’s text.
@property(nonatomic, getter=isEnabled) BOOL enabled
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.
UILabel.hThe font of the text.
@property(nonatomic, retain) UIFont *font
This property applies to the entire text string. 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.
UILabel.hA Boolean value indicating whether the receiver should be drawn with a highlight.
@property(nonatomic, getter=isHighlighted) BOOL highlighted
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.
UILabel.hThe highlight color applied to the label’s text.
@property(nonatomic, retain) UIColor *highlightedTextColor
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 .
UILabel.hThe technique to use for wrapping and truncating the label’s text.
@property(nonatomic) UILineBreakMode lineBreakMode
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 UILineBreakModeTailTruncation by default.
UILabel.hThe size of the smallest permissible font with which to draw the label’s text.
@property(nonatomic) CGFloat minimumFontSize
When drawing text that might not fit within the bounding rectangle of the label, 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 label, you should always increase this value.
UILabel.hThe maximum number of lines to use for rendering text.
@property(nonatomic) NSInteger numberOfLines
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.
UILabel.hThe shadow color of the text.
@property(nonatomic, retain) UIColor *shadowColor
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.
UILabel.hThe shadow offset (measured in points) for the text.
@property(nonatomic) CGSize shadowOffset
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.
UILabel.hThe text displayed by the label.
@property(nonatomic, copy) NSString *text
This string is nil by default.
UILabel.hThe technique to use for aligning the text.
@property(nonatomic) UITextAlignment textAlignment
This property applies to the entire text string. The default value of this property is UITextAlignmentLeft.
UILabel.hThe color of the text.
@property(nonatomic, retain) UIColor *textColor
This property applies to the entire text string. 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.
UILabel.hA Boolean value that determines whether user events are ignored and removed from the event queue.
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
This property is inherited from the UIView parent class. This class changes the default value of this property to NO.
UILabel.hDraws the receiver’s text (or its shadow) in the specified rectangle.
- (void)drawTextInRect:(CGRect)rect
The rectangle in which to draw the text.
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.
UILabel.hReturns the drawing rectangle for the label’s text.
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
The bounding rectangle of the receiver.
The maximum number of lines to use for the label. The value 0 indicates there is no maximum number of lines and that the rectangle should encompass all of the text.
The computed 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.
UILabel.hLast updated: 2009-03-26