NSTextContainer Class Reference for iOS

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 7.0 and later.
Declared in
NSTextContainer.h

Overview

The NSTextContainer class defines a region in which text is laid out. An NSLayoutManager object uses one or more NSTextContainer objects to determine where to break lines, lay out portions of text, and so on. An NSTextContainer object defines rectangular regions, and you can define exclusion paths inside the text container's bounding rectangle so that text flows around the exclusion path as it is laid out. You can create subclasses that define regions of nonrectangular shapes, such as circular regions.

NSTextContainer, NLayoutManager, and NSTextStorage can be accessed from subthreads as long as the app guarantees the access from a single thread.

Tasks

Creating a Text Container

Laying Out Text

Defining the Container Shape

Synchronizing with the Text View

Constraining Layout

Properties

exclusionPaths

An array of UIBezierPath objects representing the exclusion paths inside the receiver's bounding rectangle. Default value: nil.

@property(copy, nonatomic) NSArray *exclusionPaths
Discussion

When the layout manager proposes a line fragment rectangle intersecting one of the regions defined by the exclusion paths, the text container returns an adjusted line fragment rectangle excluding that region.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

heightTracksTextView

Controls whether the receiver adjusts the height of its bounding rectangle when its text view is resized. Default value: NO.

@property(nonatomic) BOOL heightTracksTextView
Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

layoutManager

Accessor for the NSLayoutManager object owning the receiver.

@property(assign, nonatomic) NSLayoutManager *layoutManager
Discussion

Avoid assigning a layout manager directly through this property. Adding the text container to a layout manager through the NSLayoutManager method addTextContainer: assigns the new layout manager to this property.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

lineBreakMode

Defines the behavior of the last line inside the text container. Default value: NSLineBreakByWordWrapping.

@property(nonatomic) NSLineBreakMode lineBreakMode
Discussion

The NSLineBreakMode constants specify what happens when a line is too long for its container. For example, wrapping can occur and word boundaries (the default) or character boundaries, or the line can be clipped or truncated.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

lineFragmentPadding

The amount by which text is inset within line fragment rectangles, in points. Default value: 5.0.

@property(nonatomic) CGFloat lineFragmentPadding
Discussion

The padding appears at the beginning and end of the line fragment rectangles. This value is utilized by the layout manager for determining the layout width.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

maximumNumberOfLines

The maximum number of lines that can be stored in the receiver. Default value: 0 (no limit).

@property(nonatomic) NSUInteger maximumNumberOfLines
Discussion

This value is utilized by the layout manager to determine the maximum number of lines associated with the text container.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

size

Controls the size of the receiver’s bounding rectangle. Default value: CGSizeZero.

@property(nonatomic) CGSize size
Discussion

This property defines the maximum size for the layout area returned from lineFragmentRectForProposedRect:atIndex:writingDirection:remainingRect:. A value of 0.0 or less means no limitation.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

widthTracksTextView

Controls whether the receiver adjusts the width of its bounding rectangle when its text view is resized. Default value: NO.

@property(nonatomic) BOOL widthTracksTextView
Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h

Instance Methods

initWithSize:

Initializes a text container with a specified bounding rectangle.

- (id)initWithSize:(CGSize)size
Parameters
size

The size of the text container's bounding rectangle.

Return Value

The size of the text container's bounding rectangle.

Discussion

The new text container must be added to an NSLayoutManager object before it can be used. The text container must also have an UITextView object set for text to be displayed. This method is the designated initializer for the NSTextContainer class.

Availability
  • Available in iOS 7.0 and later.
See Also
Declared In
NSTextContainer.h

lineFragmentRectForProposedRect:atIndex:writingDirection:remainingRect:

Returns the bounds of a line fragment rectangle inside the receiver for the proposed rectangle.

- (CGRect)lineFragmentRectForProposedRect:(CGRect)proposedRect atIndex:(NSUInteger)characterIndex writingDirection:(NSWritingDirection)baseWritingDirection remainingRect:(CGRect *)remainingRect
Parameters
proposedRect

A rectangle in which to lay out text proposed by the layout manager.

characterIndex

The character location inside the text storage for the line fragment being processed.

baseWritingDirection

The direction of advancement for line fragments inside a visual horizontal line. The values passed into the method are either NSWritingDirectionLeftToRight or NSWritingDirectionRightToLeft.

remainingRect

The remainder of the proposed rectangle that was excluded from returned rectangle. It can be passed in as the proposed rectangle for the next iteration.

Return Value

The line fragment rectangle calculated from the proposed rectangle and the receiver’s definition of the layout region.

Discussion

The bounds of the line fragment rectangle are determined by the intersection of proposedRect and the receiver's bounding rectangle defined by its size property. The regions defined by the exclusionPaths property are excluded from the return value. It is possible that proposedRect can be divided into multiple line fragments due to exclusion paths. In that case, remainingRect returns the remainder that can be passed in as the proposed rectangle for the next iteration.

This method can be overridden by subclasses for further text container region customization.

Availability
  • Available in iOS 7.0 and later.
Declared In
NSTextContainer.h