Mac Developer Library

Developer

AppKit Framework Reference NSTextContainer Class Reference

Options
Deployment Target:

On This Page
Language:

NSTextContainer

Inheritance


Import Statement


Swift

import AppKit

Objective-C

@import AppKit;

Availability


Available in OS X v10.0 and later.

The NSTextContainer class defines a region where text is laid out. An NSLayoutManager uses NSTextContainer to determine where to break lines, lay out portions of text, and so on. NSTextContainer defines rectangular regions, but you can create subclasses that define regions of other shapes, such as circular regions, regions with holes in them, or regions that flow alongside graphics.

  • Initializes a text container with a specified bounding rectangle.

    Declaration

    Swift

    init(containerSize aSize: NSSize)

    Objective-C

    - (instancetype)initWithContainerSize:(NSSize)aSize

    Parameters

    aSize

    The size of the text container's bounding rectangle.

    Return Value

    The newly initialized text container.

    Discussion

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

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

    See Also

    – addTextContainer: (NSLayoutManager)
    – setTextView:

  • Sets the receiver’s layout manager.

    Declaration

    Swift

    unowned(unsafe) var layoutManager: NSLayoutManager?

    Objective-C

    @property(assign) NSLayoutManager *layoutManager

    Parameters

    aLayoutManager

    The new layout manager.

    Discussion

    This method is invoked automatically when you add a text container to a layout manager; you should never need to invoke it directly, but might want to override it. If you want to replace the layout manager for an established group of text system objects, use replaceLayoutManager:.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

    See Also

    – addTextContainer: (NSLayoutManager)
    – layoutManager

  • Returns the receiver’s layout manager.

    Declaration

    Swift

    unowned(unsafe) var layoutManager: NSLayoutManager?

    Objective-C

    @property(assign) NSLayoutManager *layoutManager

    Return Value

    The text container's layout manager.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Replaces the layout manager for the group of text system objects containing the receiver.

    Declaration

    Swift

    func replaceLayoutManager(_ aLayoutManager: NSLayoutManager?)

    Objective-C

    - (void)replaceLayoutManager:(NSLayoutManager *)aLayoutManager

    Parameters

    aLayoutManager

    The new layout manager.

    Discussion

    All text containers and text views sharing the original layout manager share the new layout manager. This method makes all the adjustments necessary to keep these relationships intact, unlike setLayoutManager:.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Sets the receiver’s text view.

    Declaration

    Swift

    var textView: NSTextView?

    Objective-C

    @property(strong) NSTextView *textView

    Parameters

    aTextView

    The new text view.

    Discussion

    This method sends setTextContainer: to aTextView to complete the association of the text container and text view.

    Because you usually specify a text container when you create a text view, you should rarely need to invoke this method. A text container doesn’t need a text view to calculate line fragment rectangles, but must have one to display text.

    You can use this method to disconnect a text view from a group of text system objects by sending this message to its text container and passing nil as aTextView.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Returns the receiver’s text view.

    Declaration

    Swift

    var textView: NSTextView?

    Objective-C

    @property(strong) NSTextView *textView

    Return Value

    The receiver's text view, or nil if it has none.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Overridden by subclasses to calculate and return the longest rectangle available in the proposed rectangle for displaying text, or NSZeroRect if there is none according to the receiver’s region definition.

    Declaration

    Swift

    func lineFragmentRectForProposedRect(_ proposedRect: NSRect, sweepDirection sweepDirection: NSLineSweepDirection, movementDirection movementDirection: NSLineMovementDirection, remainingRect remainingRect: NSRectPointer) -> NSRect

    Objective-C

    - (NSRect)lineFragmentRectForProposedRect:(NSRect)proposedRect sweepDirection:(NSLineSweepDirection)sweepDirection movementDirection:(NSLineMovementDirection)movementDirection remainingRect:(NSRectPointer)remainingRect

    Parameters

    proposedRect

    The proposed rectangle in which to layout text.

    sweepDirection

    The line sweep direction.

    movementDirection

    The line movement direction.

    remainingRect

    Upon return, the unused, possibly shifted, portion of proposedRect that’s available for further text, or NSZeroRect if there is no remainder.

    Return Value

    The longest rectangle available in the proposed rectangle for displaying text, or NSZeroRect if there is none according to the receiver’s region definition.

    Discussion

    There is no guarantee as to the width of the proposed rectangle or to its location. For example, the proposed rectangle is likely to be much wider than the width of the receiver. The receiver should examine proposedRect to see that it intersects its bounding rectangle and should return a modified rectangle based on sweepDirection and movementDirection, whose possible values are listed in the class description. If sweepDirection is NSLineSweepRight, for example, the receiver uses this information to trim the right end of proposedRect as needed rather than the left end.

    If proposedRect doesn’t completely overlap the region along the axis of movementDirection and movementDirection isn’t NSLineDoesntMove, this method can either shift the rectangle in that direction as much as needed so that it does completely overlap, or return NSZeroRect to indicate that the proposed rectangle simply doesn’t fit.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • isSimpleRectangularTextContainer - isSimpleRectangularTextContainer Available in OS X v10.0 through OS X v10.9

    Overridden by subclasses to return whether 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.

    Declaration

    Objective-C

    - (BOOL)isSimpleRectangularTextContainer

    Return Value

    YEStrue if 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, NOfalse otherwise.

    Discussion

    A text container whose shape changes can return YEStrue if its region is currently a simple rectangle, but when its shape does change it must send textContainerChangedGeometry: to its layout manager so the layout can be recalculated.

    The default NSTextContainer implementation of this method returns YEStrue.

    Import Statement

    Objective-C

    @import AppKit;

    Availability

    Available in OS X v10.0 through OS X v10.9.

  • Overridden by subclasses to return whether a point lies within the receiver’s region or on the region’s edge—not simply within its bounding rectangle.

    Declaration

    Swift

    func containsPoint(_ aPoint: NSPoint) -> Bool

    Objective-C

    - (BOOL)containsPoint:(NSPoint)aPoint

    Parameters

    aPoint

    The point in question.

    Return Value

    YEStrue if aPoint lies within the receiver’s region or on the region’s edge—not simply within its bounding rectangle—NOfalse otherwise.

    Discussion

    For example, if the receiver defines a donut shape and aPoint lies in the hole, this method returns NOfalse. This method can be used for hit testing of mouse events.

    The default NSTextContainer implementation merely checks that aPoint lies within its bounding rectangle.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • These constants describe the progression of text on a page. The typesetter decides which way text is supposed to flow and passes these values as arguments to the text container, which uses them to calculate the next line rectangle.

    Declaration

    Swift

    enum NSLineSweepDirection : UInt { case Left case Right case Down case Up }

    Objective-C

    enum { NSLineSweepLeft = 0, NSLineSweepRight = 1, NSLineSweepDown = 2, NSLineSweepUp = 3 }; typedef NSUInteger NSLineSweepDirection;

    Constants

    • Left

      NSLineSweepLeft

      Characters move from right to left.

      Available in OS X v10.0 and later.

    • Right

      NSLineSweepRight

      Characters move from left to right.

      Available in OS X v10.0 and later.

    • Down

      NSLineSweepDown

      Characters move from top to bottom.

      Available in OS X v10.0 and later.

    • Up

      NSLineSweepUp

      Characters move from bottom to top.

      Available in OS X v10.0 and later.

    Discussion

    Line sweep is the direction text progresses within a line. See Text System Storage Layer Overview.

    The only values currently used by the supplied typesetters are NSLineSweepRight and NSLineMovesDown. An NSTextContainer subclass should be prepared to deal with any value, and an NSTypesetter subclass should be able to use any of them.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.

  • Line movement is the direction in which lines move. See Text System Storage Layer Overview.

    Declaration

    Swift

    enum NSLineMovementDirection : UInt { case DoesntMove case MovesLeft case MovesRight case MovesDown case MovesUp }

    Objective-C

    enum { NSLineDoesntMove = 0, NSLineMovesLeft = 1, NSLineMovesRight = 2, NSLineMovesDown = 3, NSLineMovesUp = 4 }; typedef NSUInteger NSLineMovementDirection;

    Constants

    • MovesLeft

      NSLineMovesLeft

      Lines move from right to left.

      Available in OS X v10.0 and later.

    • MovesRight

      NSLineMovesRight

      Lines move from left to right.

      Available in OS X v10.0 and later.

    • MovesDown

      NSLineMovesDown

      Lines move from top to bottom.

      Available in OS X v10.0 and later.

    • MovesUp

      NSLineMovesUp

      Lines move from bottom to top.

      Available in OS X v10.0 and later.

    • DoesntMove

      NSLineDoesntMove

      Line has no movement.

      Available in OS X v10.0 and later.

    Import Statement

    Objective-C

    @import AppKit;

    Swift

    import AppKit

    Availability

    Available in OS X v10.0 and later.