我正在制作一个格式化剧本的应用程序,我正在使用 NSAttributedString 来格式化输入到 UITextView 中的文本,但是有些行靠得太近了。
我想知道是否有人可以提供有关如何更改这些行之间的边距以使它们之间有更多空间的代码示例或提示。
下面是另一个桌面编剧程序的图片,它演示了我的意思,请注意在每个位之前都有一点空间,上面写着“DOROTHY”。
最佳答案
以下示例代码使用段落样式来调整文本段落之间的间距。
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.paragraphSpacing = 0.25 * font.lineHeight;
NSDictionary *attributes = @{NSFontAttributeName:font,
NSForegroundColorAttributeName:[UIColor whiteColor],
NSBackgroundColorAttributeName:[UIColor clearColor],
NSParagraphStyleAttributeName:paragraphStyle,
};
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
要有选择地调整某些段落的间距,请仅将段落样式应用于这些段落。
希望这对您有所帮助。
关于ios - 如何在 NSAttributedString 中为行添加间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21370495/