简体   繁体   中英

paragraphSpacingBefore from NSParagraphStyle does not work

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.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM