<!--
{
  "availability" : [
    "macOS: 12.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextLayoutFragment",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextLayoutFragment"
  },
  "title" : "NSTextLayoutFragment"
}
-->

# NSTextLayoutFragment

A class that represents the layout fragment typically corresponding to a rendering surface, such as a layer or view subclass.

```
class NSTextLayoutFragment
```

## Topics

### Creating a layout fragment

[`-  initWithCoder:`](/documentation/AppKit/NSTextLayoutFragment/init(coder:))

Creates a new layout fragment with the coder you provide.

[`-  initWithTextElement:range:`](/documentation/AppKit/NSTextLayoutFragment/init(textElement:range:))

Create a new layout fragment using the provided text element and range.

### Getting line fragments

[`textLineFragments`](/documentation/AppKit/NSTextLayoutFragment/textLineFragments)

An array of text line fragments.

[`EnumerationOptions`](/documentation/AppKit/NSTextLayoutFragment/EnumerationOptions)

Values that describe options for enumerating text layout fragments.

[`-  textLineFragmentForTextLocation:isUpstreamAffinity:`](/documentation/AppKit/NSTextLayoutFragment/textLineFragment(for:isUpstreamAffinity:))

Returns a text line fragment from a specific text location in the document.

[`-  textLineFragmentForVerticalOffset:requiresExactMatch:`](/documentation/AppKit/NSTextLayoutFragment/textLineFragment(forVerticalOffset:requiresExactMatch:))

Returns the text line fragment for the vertical offset you provide, or the closest text line fragment beyond the vertical offset.

### Getting element information

[`state`](/documentation/AppKit/NSTextLayoutFragment/state-swift.property)

The layout information state.

[`State`](/documentation/AppKit/NSTextLayoutFragment/State-swift.enum)

Values that describe the possible layout states.

[`rangeInElement`](/documentation/AppKit/NSTextLayoutFragment/rangeInElement)

The range inside the text element relative to the document origin.

[`textElement`](/documentation/AppKit/NSTextLayoutFragment/textElement)

The parent text element.

### Accessing the layout manager

[`textLayoutManager`](/documentation/AppKit/NSTextLayoutFragment/textLayoutManager)

The layout manager for this text layout fragment.

### Drawing the fragment and attachments

[`layoutFragmentFrame`](/documentation/AppKit/NSTextLayoutFragment/layoutFragmentFrame)

The rectangle the framework uses for tiling the layout fragment inside the target layout coordinate system.

[`renderingSurfaceBounds`](/documentation/AppKit/NSTextLayoutFragment/renderingSurfaceBounds)

The bounds defining the area required for rendering the contents.

[`-  drawAtPoint:inContext:`](/documentation/AppKit/NSTextLayoutFragment/draw(at:in:))

Renders the visual representation of this element in the specified graphics context.

[`-  invalidateLayout`](/documentation/AppKit/NSTextLayoutFragment/invalidateLayout())

Invalidates any layout information associated with the text layout fragment.

[`textAttachmentViewProviders`](/documentation/AppKit/NSTextLayoutFragment/textAttachmentViewProviders)

The attachment view provider associated with the text layout fragment.

[`-  frameForTextAttachmentAtLocation:`](/documentation/AppKit/NSTextLayoutFragment/frameForTextAttachment(at:))

Returns the frame in the text layout fragment coordinate system for the attachment at the location you specify.

### Accessing the layout processing queue

[`layoutQueue`](/documentation/AppKit/NSTextLayoutFragment/layoutQueue)

The queue on which the framework dispatches layout operations.

### Defining margins and padding

[`bottomMargin`](/documentation/AppKit/NSTextLayoutFragment/bottomMargin)

The amount of space reserved during paragraph layout between the bottom of the last line in the paragraph and the bottom of the text layout fragment.

[`leadingPadding`](/documentation/AppKit/NSTextLayoutFragment/leadingPadding)

The amount of margin space reserved during paragraph layout between the leading edge of the text layout fragment and the start of the lines in the paragraph.

[`topMargin`](/documentation/AppKit/NSTextLayoutFragment/topMargin)

The amount of space reserved during paragraph layout between the top of the text layout fragment and the top of the first line in the paragraph.

[`trailingPadding`](/documentation/AppKit/NSTextLayoutFragment/trailingPadding)

The amount of margin space reserved during paragraph layout between the end of the lines in the paragraph and the trailing edge of the text layout fragment.

## Relationships

### Conforms To

[`NSTextViewportRenderingSurfaceKey`](/documentation/AppKit/NSTextViewportRenderingSurfaceKey)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)