iOS CTFramesetterCreateWithAttributedString大小不正确 [英] iOS CTFramesetterCreateWithAttributedString not sizing correctly

查看:698
本文介绍了iOS CTFramesetterCreateWithAttributedString大小不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序需要几个不同的文本文件并将它们串在一起,然后从中构建一个pdf。我需要添加文件的文本添加时间,因此可以在每个文件的文本中添加超链接。我已经取得了一些进展,并使其能够几乎准确地显示,但是CTFramesetterCreateWithAttributedString的大小似乎不正确。

I am building an app that takes a few different text files and strings them together, then builds a pdf out of them. I needed to add the file's text add separate times so I can add in hyperlinks to the text from each file. I have made some progress and got it to display almost accurately, but it appears CTFramesetterCreateWithAttributedString is not sizing correctly.

例如,三个文件中的第一个显示太多,换句话说,在文本结束之后,后面是空白页。第二个文本文件显示很好,但是第三个文本文件从开始就丢失了很大一部分。我不认为这与文件本身有任何关系,因为如果我反转呈现文件的顺序,则会遇到相同的问题。第一个文件(无论是哪个文件)末尾都有空白页,而第三个文件缺少第一页左右。有人知道是什么原因引起的吗?

For example, the first of the three files displays too much, in other words, after the text ends there is a blank page that comes after it. The second text file displays just fine, but the third has a large chunk from the beginning missing. I do not believe it has anything to do with the files themselves, because I get the same problem if I reverse the order in which the files are rendered. The first file (whichever file it is) has a blank page at the end, and the third file has the first page or so missing. Does anyone know what could be causing this phenomenon?

- (void)savePDFFile:(NSString *)file_Name
{ 
 NSString *homeDir = NSHomeDirectory();
 NSString *saveDirectory = [NSString stringWithFormat: @"%@/%@", homeDir, @"Documents/"]; 


 NSArray *fileAr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:saveDirectory error:nil];
 NSMutableArray *textArray = [[NSMutableArray alloc] init];
 NSInteger currentPage = 0;
 NSInteger currentFile = 0;


 for (NSString *string in fileAr) {
    if([string hasSuffix:@"txt"]){
        NSString *file = [NSString stringWithFormat: @"%@/%@", saveDirectory, string];
        NSString *text =[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
        completeString = [NSString stringWithFormat:@"%@%@", completeString, text];
    }
 }

        NSString* pdfFileName = file_Name;

        // Create the PDF context using the default page size of 612 x 792.
        UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);


        CFRange currentRange = CFRangeMake(0, 0);
        for (NSString *string in textArray){
        BOOL done = NO;
            NSString *thisText = [textArray objectAtIndex:currentFile];
            CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)thisText, NULL);                                
            CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)currentText);
            currentPage = currentPage;
        do {
            // Mark the beginning of a new page.
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

            // Draw a page number at the bottom of each page
            currentPage++;
            [self drawPageNumber:currentPage];



            // Render the current page and update the current range to
            // point to the beginning of the next page.
            currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter];
            // If we're at the end of the text, exit the loop.
            if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))
                done = YES;

        } while (!done);
            currentFile ++;
            CFRelease(framesetter);
            CFRelease(currentText);
        }

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


}


// Use Core Text to draw the text in a frame on the page.
- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange
   andFramesetter:(CTFramesetterRef)framesetter
  {
 // Get the graphics context.
CGContextRef    currentContext = UIGraphicsGetCurrentContext();

// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

// Create a path object to enclose the text. Use 72 point
// margins all around the text.
CGRect    frameRect = CGRectMake(72, 72, 500, 648);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);

// Get the frame that will do the rendering.
// The currentRange variable specifies only the starting point. The framesetter
// lays out as much text as will fit into the frame.
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);

// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, 792);
CGContextScaleCTM(currentContext, 1.0, -1.0);

// Draw the frame.
CTFrameDraw(frameRef, currentContext);

// Update the current range based on what was drawn.
currentRange = CTFrameGetVisibleStringRange(frameRef);
currentRange.location += currentRange.length;
currentRange.length = 0;
//    CFRelease(frameRef);

return currentRange;
}


推荐答案

我知道了。

我放置了 currentPage = currentPage;在我的代码中只是为了在设置框架设定器后有一个断点,以便我可以观看它。尚未完全掌握调试器。显然,这并没有引起问题,但是如果我用 currentRange.location = 0;替换该行代码,则在每次循环后重置范围。问题解决了。

I placed "currentPage = currentPage;" in my code just to have a spot to put a breakpoint after the framesetter had been set so I could watch it. Haven't quite mastered the debugger just yet. It wasn't causing a problem, obviously, but if I replaced that line of code with "currentRange.location = 0;", resetting the range after each loop. Problem solved. Strange, as originally it was the resetting of currentRange that was causing the problem.

无论如何,上面列出的更改以及这个问题(减去最后四行),问题已解决!

Anyway, with the change I list above, along with the second answer to this question (minus the last four lines), the problem is solved!

这篇关于iOS CTFramesetterCreateWithAttributedString大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆