2

The paragraphSpacingBefore property of NSParagraphStyle does not seem to do anything. I'm pretty sure it worked in a previous project, but I don't see anything different in my old code that what I'm doing now. Here's my sample code:

textView.contentInset = UIEdgeInsetsZero;

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;
paragraphStyle.firstLineHeadIndent = 22;
paragraphStyle.paragraphSpacingBefore = 120;

NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
[attributes setObject:[UIColor clearColor] forKey:NSBackgroundColorAttributeName];
[attributes setObject:[UIFont fontWithName:@"Helvetica" size:19] forKey:NSFontAttributeName];
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];

textView.attributedText = [[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda." attributes:attributes];

Any suggestions would be greatly appreciated.

Thanks.

Casey Perkins
  • 1,853
  • 2
  • 23
  • 41

1 Answers1

0

Have you binded your UITextView in the Interface Builder to your variable textView?

Use Objective-C Literals.

Code Updated

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textView.backgroundColor = [UIColor grayColor];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentJustified;
    paragraphStyle.firstLineHeadIndent = 22;
    paragraphStyle.paragraphSpacingBefore = 120;

    NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor blackColor],
                                                NSBackgroundColorAttributeName: [UIColor clearColor],
                                                NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:13],
                                                NSParagraphStyleAttributeName: paragraphStyle
                                                };

    self.textView.attributedText = [[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda." attributes:attributes];

}

Result:

enter image description here

Aleksey Potapov
  • 3,683
  • 5
  • 42
  • 65
  • Thank you for your response. The UITextView is bound, and the first line indent appears in the text. Unfortunately, no matter which way I express the attributes variable, the paragraph spacing before does not appear. – Casey Perkins Apr 26 '16 at 16:51
  • So first paragraph exists. But you want to have new indent for each parapraph? – Aleksey Potapov Apr 26 '16 at 18:46
  • Only those which commence a new chapter. None of that is represented in the test code above; that code is just for the purpose of demonstrating my problem with making paragraphSpacingBefore work. – Casey Perkins Apr 26 '16 at 20:04
  • 1
    I updated the code. For me that code is worked for each new paragraph. How do you create new paragraph? You should use `\n` in your text – Aleksey Potapov Apr 26 '16 at 20:13
  • I see that you are correct. However, I was expecting it to do it for the first paragraph as well, which is not preceded by a \n. Is there a way to do this? Thanks again. – Casey Perkins Apr 26 '16 at 21:15
  • The first paragraph does not need `\n` symbol. But the next paragraphs must have `\n`. – Aleksey Potapov Apr 26 '16 at 21:52
  • But the first paragraph does not display the space before the paragraph, as hoped for. – Casey Perkins Apr 27 '16 at 16:58
  • There must be a bug. Because I tried this code and provided an image - you see paragraphs where they should be. Look deeply into your code and find there may be another issue. – Aleksey Potapov Apr 27 '16 at 17:42
  • Your picture does not display any sort of large paragraph-before spacing for the first paragraph, only the indent. – Casey Perkins Apr 27 '16 at 18:36
  • Sorry, may be there's some misunderstandings. But I don't think I get your idea right. – Aleksey Potapov Apr 27 '16 at 19:07
  • 1
    But, have you tried to put `\n` before the first paragraph? – Aleksey Potapov May 05 '16 at 12:26
  • Thanks for the "\n" advice, it was something that I've missed, saved me a lot of time! – szooky Apr 06 '17 at 12:50