ios - 如何在 NSAttributedString 中为行添加间距

标签 ios nsattributedstring

我正在制作一个格式化剧本的应用程序,我正在使用 NSAttributedString 来格式化输入到 UITextView 中的文本,但是有些行靠得太近了。

我想知道是否有人可以提供有关如何更改这些行之间的边距以使它们之间有更多空间的代码示例或提示。

下面是另一个桌面编剧程序的图片,它演示了我的意思,请注意在每个位之前都有一点空间,上面写着“DOROTHY”。

enter image description here

最佳答案

以下示例代码使用段落样式来调整文本段落之间的间距。

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/

相关文章:

ios - 多个 CALayer 动画同时执行

ios7 - iOS 7 使用 HTML 字符串作为 NSAttributedString 并设置字体?

ios - 委托(delegate)消息是否必须始终将 void 作为返回类型?

ios - MKMapKit UILabel 作为注解

来自 NSAttributedString 的 HTML

ios - 带有属性字符串的电子邮件编辑器

ios - 在 UILabel.attributedText *not* 蓝色和 *not* 下划线

ios - 在 Storyboard 中直接向 UITextView 添加属性文本

iphone - UIScrollView - 一种使一个 ScrollView 小于另一个 ScrollView 的方法?

ios - Apple 示例代码中的 NavigationView