jroyce
jroyce

Reputation: 2148

How do I make an array of sentences?

I am trying to resolve an issue to determine how much text to print out to a PDF to fill one page and then the create a new page.

Here is what I have so far:

        CGSize maximumSize = CGSizeMake(stringSize.width, 999999999);
        CGSize expectedSize = [cleanText sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
        NSInteger totalPages = ceil(expectedSize.height / stringSize.height);
        NSInteger linesWithoutClipping = floor(stringSize.height / font.lineHeight);
        CGFloat optimalPageHeight = linesWithoutClipping * font.lineHeight;
        NSLog(@"optimalPageHeight = %f", optimalPageHeight);

        //CHECK IF THE HEIGHT IS BIGGER THAN
        if(renderingRect.size.height > rect.size.height){
            NSLog(@"LETS DO THIS ON TWO PAGES");
            [cleanText drawInRect:rect withAttributes:dictionary];
            onePageofText = cleanText;
            done = NO;
        } else {
            onePageofText = cleanText;
            done = YES;
        }
        //Draw some text for the page.
        [self drawText:onePageofText];
    } 
    while (!done);

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();

I would like to try looping through the cleanText by sentence and checking each sentence before outputting it, to see if it still fits on the page. Is there a straightforward way to do this?

Upvotes: 1

Views: 49

Answers (2)

mirap
mirap

Reputation: 1266

This will give you more precise output (including "! " and "? "):

    [mutstri enumerateSubstringsInRange:NSMakeRange(0, [mutstri length])
                                options:NSStringEnumerationBySentences
                             usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

                                 NSLog(@"%@", substring);

                             }];

Upvotes: 1

citysurrounded
citysurrounded

Reputation: 674

Have you tried using -[NSString componentsSeparatedByString:]?

For example:

NSArray *sentences = [cleanText componentsSeparatedByString:@". "];

The only stipulation with this is that cleanText must be a NSString.

See documentation here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/

Upvotes: 2

Related Questions