ios - 如何制作一系列句子?

标签 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并在输出每个句子之前对其进行检查,以查看它是否仍适合页面。有没有简单的方法可以做到这一点?

最佳答案

您是否尝试过使用-[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/

相关文章:

javascript - iPhone 上 iOS Safari 中的转换会导致意外行为

android - 在用户不知情的情况下用基于 cordova/phonegap 的应用程序交换现有的 native 应用程序

iphone - 为什么我无法在我的 iOS 应用程序中对 Facebook Chat 进行身份验证?

iphone - 如何创建五个带标签的 UIPickerView?以及如何在 iPhone 中为每个 UIPickerView 赋予不同的名称?

c++ - 通过指针制作字符串

ios - 如何更改 SwiftUI 中导航栏的背景颜色?

ios - 如何为 2 个不同的 UIImageView 调用 TouchesMoved

iphone - 旋转 CGPath 而不改变其位置

java - 在 Java 中,如何将 XML 解析为字符串而不是文件?

php - 正则表达式仅按最后一个空白字符拆分字符串