如何制作一系列句子?

时间:2015-11-22 03:20:25

标签: ios objective-c string

我正在尝试解决一个问题,以确定要打印到PDF以填充一个页面然后创建新页面的文本数量。

这是我到目前为止所做的:

        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();

我想尝试逐句循环cleanText并在输出之前检查每个句子,看它是否仍适合页面。有一种直截了当的方法吗?

2 个答案:

答案 0 :(得分:2)

您是否尝试过使用-[NSString componentsSeparatedByString:]

例如:

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

唯一的规定是cleanText必须是NSString。

请参阅此处的文档: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/

答案 1 :(得分:1)

这将为您提供更精确的输出(包括“!”和“?”):

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

                                 NSLog(@"%@", substring);

                             }];