Skip to content

NSMutableParagraphStyle ​

NSMutableParagraphStyle represents a set of paragraph styles that can be changed. It has all the properties of NSParagraphStyle, and they are all writable on it. Once set up, it goes into an attributed string as the paragraphStyle attribute.

What goes into the attributed string is a copy taken at that moment: changing this object afterward doesn't affect attributed strings already created. So set up the style first, then create the attributed string, and finally give it to the view.

NSMutableParagraphStyle fires no events. Its values are only changed by the app's own code, so there is no change notification.

Inheritance: EventTarget → NSObject → NSParagraphStyle → NSMutableParagraphStyle

For how to adjust line spacing and alignment, see the guide Displaying styled text.

Constructor ​

NSMutableParagraphStyle()

Creates a mutable paragraph style with all properties at their defaults.

Instance properties ​

All readable and writable. The defaults are the same as NSParagraphStyle's default style.

PropertyDescription
alignmentAlignment
lineSpacingExtra space added between lines
paragraphSpacingSpace left after the end of the paragraph
paragraphSpacingBeforeSpace left before the start of the paragraph
firstLineHeadIndentHow far the first line is indented from the left
headIndentHow far lines other than the first are indented from the left
tailIndentThe right edge of the lines
lineBreakModeWhat happens when a line doesn't fit
minimumLineHeightLower limit of the line height
maximumLineHeightUpper limit of the line height
lineHeightMultipleLine height as a multiple of the font's natural line height
baseWritingDirectionWhich side the paragraph starts from
hyphenationFactorHow aggressively words are hyphenated
defaultTabIntervalDefault interval between tab stops
allowsDefaultTighteningForTruncationWhether letter spacing is tightened before truncating

Rules for invalid values:

  • Lengths and multipliers other than tailIndent can't be negative: TypeError: lineSpacing must be a non-negative number.
  • hyphenationFactor out of range: TypeError: hyphenationFactor must be between 0 and 1.
  • Writing a value not in the table to alignment, lineBreakMode, or baseWritingDirection doesn't throw: the property keeps its previous value, and a warning listing the valid values is printed to the console.

Instance methods ​

Also inherits copy() and mutableCopy() from NSParagraphStyle.

MethodDescription
setParagraphStyle()Copies all property values from another paragraph style

Examples ​

Centered, with larger line spacing ​

js
const style = new NSMutableParagraphStyle();
style.alignment = 'center';
style.lineSpacing = 8;

const label = new UILabel();
label.numberOfLines = 0;
label.attributedText = new NSAttributedString('First line\nSecond line', {
  font: UIFont.systemFontOfSize(17),
  paragraphStyle: style,
});

Effect (not verified in the simulator): The two lines of text are centered, with 8 pt more space between them than usual.

Corresponding iOS API ​

ObjCJS
[[NSMutableParagraphStyle alloc] init]new NSMutableParagraphStyle()
style.alignment = NSTextAlignmentCenterstyle.alignment = 'center'
style.lineBreakMode = NSLineBreakByTruncatingTailstyle.lineBreakMode = 'byTruncatingTail'
[style setParagraphStyle:other]style.setParagraphStyle(other)

See also ​