NSMutableParagraphStyle
NSMutableParagraphStyle 表示一组可以改的段落样式。它有 NSParagraphStyle 的全部属性,在它上面都可写。设好之后作为 paragraphStyle 属性放进富文本。
放进富文本的是当时的一份副本:之后再改这个对象,已经建好的富文本不变。所以先把样式设好,再建富文本,最后交给视图。
NSMutableParagraphStyle 不派发事件。它的值只会被 App 自己的代码修改,所以没有变化通知。
继承关系:EventTarget → NSObject → NSParagraphStyle → NSMutableParagraphStyle
调行距和对齐的写法见指南显示带样式的文字。
构造函数
创建一个可以改的段落样式,属性都是默认值。
实例属性
全部可读、可写。默认值和 NSParagraphStyle 的默认样式相同。
| 属性 | 说明 |
|---|---|
alignment | 对齐 |
lineSpacing | 行与行之间额外加的距离 |
paragraphSpacing | 这一段结束后空出的距离 |
paragraphSpacingBefore | 这一段开始前空出的距离 |
firstLineHeadIndent | 第一行左边缩进多少 |
headIndent | 第一行以外的各行左边缩进多少 |
tailIndent | 行的右边界 |
lineBreakMode | 一行放不下时怎么处理 |
minimumLineHeight | 行高的下限 |
maximumLineHeight | 行高的上限 |
lineHeightMultiple | 行高是字体自然行高的几倍 |
baseWritingDirection | 这一段从哪边开始排 |
hyphenationFactor | 断字的积极程度 |
defaultTabInterval | 制表符之间的默认间隔 |
allowsDefaultTighteningForTruncation | 截断之前是否先收紧字距 |
写错时的规则:
- 除
tailIndent以外的长度和倍数不能是负数:TypeError: lineSpacing must be a non-negative number。 hyphenationFactor超出范围:TypeError: hyphenationFactor must be between 0 and 1。alignment、lineBreakMode、baseWritingDirection写了表里没有的值时不抛异常:属性保持原来的值,控制台输出一条警告,警告里列出可选的值。
实例方法
还继承 NSParagraphStyle 的 copy() 和 mutableCopy()。
| 方法 | 说明 |
|---|---|
setParagraphStyle() | 把另一个段落样式的全部属性值抄过来 |
示例
居中、加大行距
js
const style = new NSMutableParagraphStyle();
style.alignment = 'center';
style.lineSpacing = 8;
const label = new UILabel();
label.numberOfLines = 0;
label.attributedText = new NSAttributedString('第一行\n第二行', {
font: UIFont.systemFontOfSize(17),
paragraphStyle: style,
});效果(未在模拟器验证):两行文字居中,两行之间比平常多空 8 pt。
对应的 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) |
参见
NSParagraphStyleNSAttributedString:paragraphStyle属性- 显示带样式的文字