NSParagraphStyle
NSParagraphStyle represents a set of paragraph styles: alignment, line spacing, paragraph spacing, indentation, and line breaking. It is put into an attributed string as the paragraphStyle attribute and applies to every whole paragraph it covers. A paragraph is the text between two newline characters.
All properties of NSParagraphStyle are read-only. To set values, create an NSMutableParagraphStyle, or call mutableCopy().
When a paragraph style is put into an attributed string, a copy taken at that moment is stored. Changing that NSMutableParagraphStyle afterward doesn't change attributed strings that have already been created. A paragraph style read back from an attributed string is also a copy.
NSParagraphStyle fires no events. Its values never change, so there are no change notifications.
Inheritance: EventTarget → NSObject → NSParagraphStyle
For how to adjust line spacing and alignment, see the guide Displaying styled text.
Constructor
Creates a default style, equal to the value of NSParagraphStyle.defaultParagraphStyle.
Static properties
NSParagraphStyle.defaultParagraphStyle read-only
The system's default paragraph style.
Instance properties
Also inherits the properties of NSObject. All are read-only; assigning to them throws TypeError.
| Property | Description |
|---|---|
alignment read-only | Alignment |
lineSpacing read-only | Extra distance added between lines |
paragraphSpacing read-only | Space left after the end of the paragraph |
paragraphSpacingBefore read-only | Space left before the start of the paragraph |
firstLineHeadIndent read-only | How far the first line is indented from the left |
headIndent read-only | How far the lines other than the first are indented from the left |
tailIndent read-only | The right edge of the lines |
lineBreakMode read-only | What happens when a line doesn't fit |
minimumLineHeight read-only | The minimum line height |
maximumLineHeight read-only | The maximum line height |
lineHeightMultiple read-only | The line height as a multiple of the font's natural line height |
baseWritingDirection read-only | The side the paragraph starts from |
hyphenationFactor read-only | How aggressively words are hyphenated |
defaultTabInterval read-only | The default interval between tab stops |
allowsDefaultTighteningForTruncation read-only | Whether letter spacing is tightened before truncating |
Instance methods
| Method | Description |
|---|---|
copy() | An immutable copy with the same values |
mutableCopy() | A mutable copy with the same values |
Examples
const style = NSParagraphStyle.defaultParagraphStyle;
console.log(style.alignment, style.lineSpacing, style.lineBreakMode);Result: the console prints natural 0 byWordWrapping.
Corresponding iOS API
| ObjC | JS |
|---|---|
NSParagraphStyle.defaultParagraphStyle | NSParagraphStyle.defaultParagraphStyle |
style.alignment = NSTextAlignmentCenter | style.alignment = 'center' (on NSMutableParagraphStyle) |
[style mutableCopy] | style.mutableCopy() |
See also
NSMutableParagraphStyle: a mutable paragraph styleNSAttributedString: theparagraphStyleattribute- Displaying styled text