我正在尝试解决一个问题,以确定要打印多少文本到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并在输出每个句子之前对其进行检查,以查看它是否仍适合页面。有没有简单的方法可以做到这一点?
最佳答案
您是否尝试过使用-[NSString componentsSeparatedByString:]
?
例如:
NSArray *sentences = [cleanText componentsSeparatedByString:@". "];
唯一的规定是cleanText必须是NSString。
请参阅此处的文档:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/
关于ios - 如何制作一系列句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33851258/