iOS Developer Library

Developer

UIKit Framework Reference NSTextContainer Class Reference for iOS

Options
Deployment Target:

On This Page
Language:

NSTextContainer

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 normally defines rectangular regions, but you can define exclusion paths inside the text container to create regions where text does not flow. You can also subclass to create text containers with nonrectangular regions.

Instances of the NSTextContainer, NLayoutManager, and NSTextStorage classes can be accessed from threads other than the main thread as long as the app guarantees access from only one thread at a time.

  • init(size:) - initWithSize: Designated Initializer

    Initializes a text container with a specified bounding rectangle.

    Declaration

    Swift

    init(size size: CGSize)

    Objective-C

    - (instancetype)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 associated UITextView object 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

    – addTextContainer: (NSLayoutManager)

  • Replaces the layout manager for the group of text system objects associated with the text container.

    Declaration

    Swift

    func replaceLayoutManager(_ newLayoutManager: NSLayoutManager)

    Objective-C

    - (void)replaceLayoutManager:(NSLayoutManager *)newLayoutManager

    Parameters

    newLayoutManager

    The new layout manager.

    Discussion

    All text containers and text views attached to the old layout manager are reassigned to the new layout manager. Unlike setting the layoutManager property directly, this method makes all the adjustments necessary to keep the text object relationships intact.

    Availability

    Available in iOS 9.0 and later.

    See Also

    layoutManager

  • The layout manager that owns the text container.

    Declaration

    Swift

    unowned(unsafe) var layoutManager: NSLayoutManager?

    Objective-C

    @property(assign, nonatomic) NSLayoutManager *layoutManager

    Discussion

    Avoid assigning a layout manager directly through this property. Instead, use the replaceLayoutManager: method when you want to replace the layout manager. The value of this property is set automatically when you add a text container to your layout manager using the addTextContainer: method.

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    func lineFragmentRectForProposedRect(_ proposedRect: CGRect, atIndex characterIndex: Int, writingDirection baseWritingDirection: NSWritingDirection, remainingRect remainingRect: UnsafeMutablePointer<CGRect>) -> CGRect

    Objective-C

    - (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.

  • A Boolean indicating whether the text container’s region is a rectangle with no holes or gaps. (read-only)

    Declaration

    Swift

    var simpleRectangularTextContainer: Bool { get }

    Objective-C

    @property(getter=isSimpleRectangularTextContainer, readonly, nonatomic) BOOL simpleRectangularTextContainer

    Discussion

    This property is YEStrue when the receiver’s region is a rectangle with no holes or gaps and whose edges are parallel to the text view's coordinate system axes. The default value of this property is NOfalse when the exclusionPaths property contains one or more items, when maximumNumberOfLines is not zero, or when you override the lineFragmentRectForProposedRect:atIndex:writingDirection:remainingRect: method.

    Availability

    Available in iOS 9.0 and later.

  • A Boolean indicating whether the text container adjusts the height of its bounding rectangle when its text view is resized.

    Declaration

    Swift

    var heightTracksTextView: Bool

    Objective-C

    @property(nonatomic) BOOL heightTracksTextView

    Discussion

    The default value of this property is NOfalse.

    Availability

    Available in iOS 7.0 and later.

  • A Boolean indicating whether the text container adjusts the width of its bounding rectangle when its text view is resized.

    Declaration

    Swift

    var widthTracksTextView: Bool

    Objective-C

    @property(nonatomic) BOOL widthTracksTextView

    Discussion

    The default value of this property is NOfalse.

    Availability

    Available in iOS 7.0 and later.

  • The maximum number of lines that can be stored in the text container.

    Declaration

    Swift

    var maximumNumberOfLines: Int

    Objective-C

    @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. The default value of this property is 0, which indicates that there is no limit.

    Availability

    Available in iOS 7.0 and later.

  • The amount by which text is inset within line fragment rectangles, measured in points.

    Declaration

    Swift

    var lineFragmentPadding: CGFloat

    Objective-C

    @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. The default value of this property is 5.0.

    Availability

    Available in iOS 7.0 and later.