我正在尝试解决一个问题,以确定要打印到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并在输出之前检查每个句子,看它是否仍适合页面。有一种直截了当的方法吗?
答案 0 :(得分:2)
您是否尝试过使用-[NSString componentsSeparatedByString:]
?
例如:
NSArray *sentences = [cleanText componentsSeparatedByString:@". "];
唯一的规定是cleanText必须是NSString。
答案 1 :(得分:1)
这将为您提供更精确的输出(包括“!”和“?”):
[mutstri enumerateSubstringsInRange:NSMakeRange(0, [mutstri length])
options:NSStringEnumerationBySentences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
NSLog(@"%@", substring);
}];