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
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.
| Property | Description |
|---|---|
alignment | Alignment |
lineSpacing | Extra space added between lines |
paragraphSpacing | Space left after the end of the paragraph |
paragraphSpacingBefore | Space left before the start of the paragraph |
firstLineHeadIndent | How far the first line is indented from the left |
headIndent | How far lines other than the first are indented from the left |
tailIndent | The right edge of the lines |
lineBreakMode | What happens when a line doesn't fit |
minimumLineHeight | Lower limit of the line height |
maximumLineHeight | Upper limit of the line height |
lineHeightMultiple | Line height as a multiple of the font's natural line height |
baseWritingDirection | Which side the paragraph starts from |
hyphenationFactor | How aggressively words are hyphenated |
defaultTabInterval | Default interval between tab stops |
allowsDefaultTighteningForTruncation | Whether letter spacing is tightened before truncating |
Rules for invalid values:
- Lengths and multipliers other than
tailIndentcan't be negative:TypeError: lineSpacing must be a non-negative number. hyphenationFactorout of range:TypeError: hyphenationFactor must be between 0 and 1.- Writing a value not in the table to
alignment,lineBreakMode, orbaseWritingDirectiondoesn'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.
| Method | Description |
|---|---|
setParagraphStyle() | Copies all property values from another paragraph style |
Examples
Centered, with larger line spacing
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
| ObjC | JS |
|---|---|
[[NSMutableParagraphStyle alloc] init] | new NSMutableParagraphStyle() |
style.alignment = NSTextAlignmentCenter | style.alignment = 'center' |
style.lineBreakMode = NSLineBreakByTruncatingTail | style.lineBreakMode = 'byTruncatingTail' |
[style setParagraphStyle:other] | style.setParagraphStyle(other) |
See also
NSParagraphStyleNSAttributedString: theparagraphStyleattribute- Displaying styled text