2012-08-28 94 views
2

在pdf文檔的兩個或多個頁面上繪製一長串文本的建議方式是什麼?iOS:在PDF文檔的多個頁面上繪製文本

當您爲單個頁面繪製太長的文本字符串時,文本會在文檔末尾被剪切。我最初的想法是計算可以在當前頁面上繪製多少字符串,然後將字符串拆分爲兩個子字符串:(1)可以在當前頁面上繪製的部分和(2)被剪切的部分。下一步將是重複這個過程,直到整個字符串被繪製。

問題仍然是如何計算在當前頁面上可以繪製多少字符串。我是否忽略了一些東西,或者這是一個相當乏味的過程,容易出錯?

回答

2

您可以嘗試通過UIMarkupTextPrintFormatter使用iOS打印子系統生成PDF。我在這裏解釋的技術:

Is there any way to generate PDF file from a XML/HTML template in iOs

我還沒有嘗試過,看看會發生什麼重。分頁,但理論上它會做正確的事情。

+0

我不知道爲正常文本,它可能工作。對於我的應用程序之一,我使用具有行和列的HTML表格使用iOS打印子系統打印到PDF。結果是在一些頁面上,表格的其中一行將隨機獲得偏移量。但是,它需要將文字移動到一個單獨的頁面。 – Zhang

2

我試圖計算一個文本一旦繪製到PDF上的真實高度,但它真的很惱人,因爲你不能真正得到「真正的」線高度。我最終放棄了CoreGraphics,並使用@TomSwift技術繪製了所有內容。比CoreGraphics更容易,你可以設置字體,文本顏色,...用CSS樣式

3

很久以前,我做了這個函數它可以幫助你,通過間距和字體,它會返回你分裂的字符串在有限的寬度和需求的大小。

- (NSMutableArray *)getSplittedString:(NSString *)largeStr:(UIFont *)font:(NSInteger)horizontalSpace:(float)width { 
    NSArray *strWords = [largeStr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    //finding space width 

    NSInteger currIndex = 0; 
    NSString *tmpStr = @""; 
    NSString *compareStr = @""; 
    NSInteger currWidth; 

    NSMutableArray *strArray = [[NSMutableArray alloc] init]; 
    NSMutableArray *strFrames = [[NSMutableArray alloc] init]; 

    while(TRUE) { 
     while(TRUE) { 
      compareStr = tmpStr; 
      compareStr = [compareStr stringByAppendingString:[strWords objectAtIndex:currIndex]]; 
      currWidth = [compareStr sizeWithFont:font].width; 
      if(currWidth < width) { 
       currIndex = currIndex + 1; 
       if(currIndex == [strWords count]) { 
        [strArray addObject:compareStr]; 
        [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:compareStr :width :font :horizontalSpace]]]; 
        break; 
       } 
       tmpStr = [NSString stringWithFormat:@"%@ ",compareStr]; 

      } else if (currWidth == width) { 
       [strArray addObject:compareStr]; 
       [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:compareStr :width :font :horizontalSpace]]]; 
       tmpStr = @""; 
       if(currIndex == [strWords count]) { 
        break; 
       } 
      } else { 
       if([compareStr rangeOfString:@" "].location == NSNotFound) { 
        while(TRUE) { 
         NSInteger loc = [self findAdjustedLocation:compareStr :width :font]; 
         if(loc == 0) { 
          tmpStr = [compareStr substringFromIndex:loc]; 
          if(currIndex == [strWords count]) { 
           [strArray addObject:tmpStr]; 
           [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]]; 
           break; 
          } 
          tmpStr = [NSString stringWithFormat:@"%@ ",tmpStr]; 
          break; 
         } else { 
          tmpStr = [compareStr substringWithRange:NSMakeRange(0, loc)]; 
          [strArray addObject:tmpStr]; 
          [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]]; 
          tmpStr = [NSString stringWithFormat:@"%@ ",[compareStr substringFromIndex:loc]]; 
          compareStr = tmpStr; 
          if ([tmpStr sizeWithFont:font].width < width) { 
           [strArray addObject:tmpStr]; 
           [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]]; 
           currIndex = currIndex + 1; 
           break; 
          } 
         } 
        } 
       } else { 
        [strArray addObject:tmpStr]; 
        [strFrames addObject:[NSValue valueWithCGRect:[self findCenterAdjustedLocation:tmpStr :width :font :horizontalSpace]]]; 
        tmpStr = @""; 
       } 
       break; 
      } 
     } 
     if(currIndex == [strWords count]) { 
      return [[NSMutableArray alloc] initWithObjects:strArray, strFrames, nil]; 
      break; 
     } 
    } 
} 
相關問題