iOS Developer Library — Prerelease

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

  • init(size:) - initWithSize: Designated Initializer

    Initializes a text container with a specified bounding rectangle.

    Declaration

    Swift

    init(size size: CGSize)

    Objective-C

    - (instancetype _Nonnull)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

    – addTextContainer: (NSLayoutManager)

  • Accessor for the NSLayoutManager object owning the receiver.

    Declaration

    Swift

    unowned(unsafe) var layoutManager: NSLayoutManager?

    Objective-C

    @property(assign, nonatomic) NSLayoutManager * _Nullable 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.

  • 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 * _Nullable)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.

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

    Declaration

    Swift

    var heightTracksTextView: Bool

    Objective-C

    @property(nonatomic) BOOL heightTracksTextView

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    var widthTracksTextView: Bool

    Objective-C

    @property(nonatomic) BOOL widthTracksTextView

    Availability

    Available in iOS 7.0 and later.

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

    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.

    Availability

    Available in iOS 7.0 and later.

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

    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.

    Availability

    Available in iOS 7.0 and later.