<!--
{
  "availability" : [
    "macOS: 14.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextLayoutFragment/textLineFragment(for:isUpstreamAffinity:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextLayoutFragment(im)textLineFragmentForTextLocation:isUpstreamAffinity:"
  },
  "title" : "textLineFragment(for:isUpstreamAffinity:)"
}
-->

# textLineFragment(for:isUpstreamAffinity:)

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

```
func textLineFragment(for textLocation: any NSTextLocation, isUpstreamAffinity: Bool) -> NSTextLineFragment?
```

## Parameters

`textLocation`

A text location that a text line fragment contains.

`isUpstreamAffinity`

A Boolean value that indicates whether the text line fragment ends at the text location you provide.

## Return Value

The text line fragment that contains or ends at the text location you provide, or `nil` if there isn’t a match.

## Discussion

Set `isUpstreamAffinity` to <doc://com.apple.documentation/documentation/Swift/true> to find a text fragment by its element range end location, such as when you enumerate over line fragments in reverse order. Set `isUpstreamAffinity` to <doc://com.apple.documentation/documentation/Swift/false> to find a text fragment that contains `textLocation`.

---

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)