Skip to content

NSTextContainer ​

NSTextContainer represents the area where text is laid out in a UITextView: how much space each line leaves on the left and right, how many lines at most, and what to do when a line doesn't fit.

Every text view comes with one, obtained from textContainer; each read returns the same object. Changing its properties changes the layout in the text view.

Inheritance: EventTarget → NSObject → NSTextContainer

Constructor ​

It can't be created with new. new NSTextContainer() throws TypeError: Illegal constructor: NSTextContainer is created by the runtime, not by new. Get it from textContainer of a UITextView.

Instance properties ​

PropertyDescription
lineFragmentPaddingHow much space each line leaves on the left and right
maximumNumberOfLinesThe maximum number of lines
lineBreakModeWhat to do when a line doesn't fit

Distance from text to the edges ​

The distance from the text to the left and right edges of the text view is the sum of two values: the left and right of the UITextView's textContainerInset, plus lineFragmentPadding. The defaults are 0 plus 5, so in a new text view the text is 5 pt from the left edge. The top and bottom distances are determined only by textContainerInset, 8 pt each by default.

Examples ​

Showing at most three lines ​

js
const input = new UITextView();
input.isScrollEnabled = false;
input.isEditable = false;
input.textContainer.maximumNumberOfLines = 3;
input.textContainer.lineBreakMode = 'byTruncatingTail';

Effect: only the first three lines are laid out, and when the third line doesn't fit it ends with an ellipsis (the ellipsis is not verified in the simulator). The content of text is unchanged; the extra content just isn't shown.

Corresponding iOS API ​

ObjCJS
textView.textContainerinput.textContainer
container.lineFragmentPadding = 0container.lineFragmentPadding = 0
container.maximumNumberOfLines = 3container.maximumNumberOfLines = 3
container.lineBreakMode = NSLineBreakByTruncatingTailcontainer.lineBreakMode = 'byTruncatingTail'

See also ​