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

# NSTextBlock

A block of text laid out in a subregion of the text container.

```
class NSTextBlock
```

## Overview

A text block appears as an attribute of a paragraph, and as part of the paragraph style. The most important subclass of [`NSTextBlock`](/documentation/AppKit/NSTextBlock) is [`NSTextTableBlock`](/documentation/AppKit/NSTextTableBlock), which represents a block of text that appears as a cell in a table. The table itself is a [`NSTextTable`](/documentation/AppKit/NSTextTable) object. All [`NSTextBlock`](/documentation/AppKit/NSTextBlock) objects reference this table, which controls their sizing and positioning.

## Topics

### Creating text blocks

[`-  init`](/documentation/AppKit/NSTextBlock/init())

Initializes and returns an empty text block object.

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

### Working with dimensions of content

[`-  setValue:type:forDimension:`](/documentation/AppKit/NSTextBlock/setValue(_:type:for:))

Sets a dimension of the text block.

[`-  valueForDimension:`](/documentation/AppKit/NSTextBlock/value(for:))

Returns the value of the specified text block dimension.

[`-  valueTypeForDimension:`](/documentation/AppKit/NSTextBlock/valueType(for:))

Returns the value type of the specified text block dimension.

[`-  setContentWidth:type:`](/documentation/AppKit/NSTextBlock/setContentWidth(_:type:))

Sets the width of the text block.

[`contentWidth`](/documentation/AppKit/NSTextBlock/contentWidth)

The width of the text block.

[`contentWidthValueType`](/documentation/AppKit/NSTextBlock/contentWidthValueType)

The type of value stored for the text block width.

[`Dimension`](/documentation/AppKit/NSTextBlock/Dimension)

The following constants specify values used by the methods [`setValue(_:type:for:)`](/documentation/AppKit/NSTextBlock/setValue(_:type:for:)), [`value(for:)`](/documentation/AppKit/NSTextBlock/value(for:)), and [`valueType(for:)`](/documentation/AppKit/NSTextBlock/valueType(for:)) to specify text block dimensions.

[`ValueType`](/documentation/AppKit/NSTextBlock/ValueType)

The following constants specify values used by the methods [`setValue(_:type:for:)`](/documentation/AppKit/NSTextBlock/setValue(_:type:for:)) and [`valueType(for:)`](/documentation/AppKit/NSTextBlock/valueType(for:)) to specify text block value types.

### Getting and setting margins, borders, and padding

[`-  setWidth:type:forLayer:`](/documentation/AppKit/NSTextBlock/setWidth(_:type:for:))

Sets the width of all edges of a specified layer of the text block.

[`-  setWidth:type:forLayer:rectEdge:`](/documentation/AppKit/NSTextBlock/setWidth(_:type:for:rectEdge:))

[`-  widthForLayer:rectEdge:`](/documentation/AppKit/NSTextBlock/width(for:rectEdge:))

[`-  widthValueTypeForLayer:rectEdge:`](/documentation/AppKit/NSTextBlock/widthValueType(for:rectEdge:))

[`Layer`](/documentation/AppKit/NSTextBlock/Layer)

The following constants specify values used by the properties and methods [`contentWidthValueType`](/documentation/AppKit/NSTextBlock/contentWidthValueType), [`setWidth(_:type:for:edge:)`](/documentation/AppKit/NSTextBlock/setWidth(_:type:for:edge:)), [`setWidth(_:type:for:)`](/documentation/AppKit/NSTextBlock/setWidth(_:type:for:)), [`width(for:edge:)`](/documentation/AppKit/NSTextBlock/width(for:edge:)), and [`widthValueType(for:edge:)`](/documentation/AppKit/NSTextBlock/widthValueType(for:edge:)) to specify text block layer values.

### Getting and setting alignment

[`verticalAlignment`](/documentation/AppKit/NSTextBlock/verticalAlignment-swift.property)

The vertical alignment of the text block.

[`VerticalAlignment`](/documentation/AppKit/NSTextBlock/VerticalAlignment-swift.enum)

The following constants specify values used by the property [`verticalAlignment`](/documentation/AppKit/NSTextBlock/verticalAlignment-swift.property) to specify vertical alignment.

### Working with color

[`backgroundColor`](/documentation/AppKit/NSTextBlock/backgroundColor)

The background color of the text block.

[`-  setBorderColor:`](/documentation/AppKit/NSTextBlock/setBorderColor(_:))

Sets the color of all borders of the text block.

[`-  setBorderColor:rectEdge:`](/documentation/AppKit/NSTextBlock/setBorderColor(_:rectEdge:))

[`-  borderColorForRectEdge:`](/documentation/AppKit/NSTextBlock/borderColor(for:)-5m8cr)

### Determining size and position of a text block

[`-  rectForLayoutAtPoint:inRect:textContainer:characterRange:`](/documentation/AppKit/NSTextBlock/rectForLayout(at:in:textContainer:characterRange:))

Returns the rectangle within which glyphs should be laid out for the specified arguments.

[`-  boundsRectForContentRect:inRect:textContainer:characterRange:`](/documentation/AppKit/NSTextBlock/boundsRect(forContentRect:in:textContainer:characterRange:))

Returns the rectangle the text in the block actually occupies, including padding, borders, and margins.

### Drawing colors and decorations

[`-  drawBackgroundWithFrame:inView:characterRange:layoutManager:`](/documentation/AppKit/NSTextBlock/drawBackground(withFrame:in:characterRange:layoutManager:))

Called by the layout manager to draw any colors and other decorations before the text is drawn.

### Deprecated

[`-  setWidth:type:forLayer:edge:`](/documentation/AppKit/NSTextBlock/setWidth(_:type:for:edge:))

Sets the width of a specified edge of a specified layer of the text block.

[`-  widthForLayer:edge:`](/documentation/AppKit/NSTextBlock/width(for:edge:))

Returns the width of an edge of a specified layer of the text block.

[`-  widthValueTypeForLayer:edge:`](/documentation/AppKit/NSTextBlock/widthValueType(for:edge:))

Returns the value type of an edge of a specified layer of the text block.

[`-  borderColorForEdge:`](/documentation/AppKit/NSTextBlock/borderColor(for:)-273pl)

[`-  setBorderColor:forEdge:`](/documentation/AppKit/NSTextBlock/setBorderColor(_:for:))

Sets the border color of the specified edge of the text block.

## Relationships

### Inherits From

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

### Conforms To

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

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

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

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

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

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

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

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

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

### Inherited By

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

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

---

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)