2013-05-08 42 views
0

我在ios中有三個nsmutable陣列。我必須使用打印機以下例子打印數據,我應該怎麼做?在ios中使用打印機打印nsmutable陣列

例如: -

0.5 1 10 
1  10 11 
2  5 22 
3  4 6 
+1

你甚至_try_? [繪圖和印刷指南的iOS:打印](http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/Printing/Printing.html) – Kreiri 2013-05-08 07:52:12

回答

0

您可以使用UIPrintInteractionController來做到這一點。

請檢查下面的代碼:

NSMutableString *stringToPrint = [[NSMutableString alloc] initWithString:@""]; 
for(int i = 0;i<[array count];i++) 
{ 
    [stringToPrint appendFormat:@"%@",[array objectAtIndex:i]]; 
    if(i%2 == 0) 
    { 
     [stringToPrint appendFormat:@"\n"]; 
    } 
    else 
    { 
     [stringToPrint appendFormat:@"\t"]; 
    } 
} 
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; 

UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
printInfo.outputType = UIPrintInfoOutputGeneral; 
printInfo.jobName = @"Midhun"; 
pic.printInfo = printInfo; 

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] 
              initWithText:stringToPrint]; 
textFormatter.startPage = 0; 
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins 
textFormatter.maximumContentWidth = 6 * 72.0; 
pic.printFormatter = textFormatter; 
pic.showsPageRange = YES; 

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = 
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) 
{ 
    if (!completed && error) 
    { 
     NSLog(@"Printing could not complete because of error: %@", error); 
    } 
}; 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{ 
    [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler]; 
} 
else 
{ 
    [pic presentAnimated:YES completionHandler:completionHandler]; 
} 

請有關詳細信息,閱讀Printing and Drawing in iOS

0

使用下面的代碼來遍歷並打印。

for (int i=0; i<[array count]; i++) { 
    NSLog(@"%@",[array objectAtIndex:i]); 
    } 
+0

不,他問如何打印三個陣列與他在他的問題中表明的格式相同。而你的答案描述瞭如何打印數組。 – 2013-05-08 08:00:34

1

對於example這些arrays

NSArray *arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil]; 
NSArray *arr1 = [NSArray arrayWithObjects:@"17",@"27",@"37",@"47",@"57",@"67", nil]; 
NSArray *arr2 = [NSArray arrayWithObjects:@"171",@"271",@"371",@"471",@"571",@"671", nil]; 

現在得到所有dataNSString

NSMutableString *str = [NSMutableString string]; 
for (int i=0; i<[arr count]; i++) 
{ 
    str = [str stringByAppendingFormat:[NSString stringWithFormat:@"%@ %@ %@\n",[arr objectAtIndex:i],[arr1 objectAtIndex:i],[arr2 objectAtIndex:i]]]; 
} 
NSLog(@"%@",str); 

使用toPdfData方法來獲取NSDataprint

NSData *data = [self toPdfData:str]; 

添加這些method

- (NSData *) toPdfData :(NSMutableString *)str 
{ 
    //calculate size 
    CGSize stringSize = [str sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320 , 999) lineBreakMode:UILineBreakModeWordWrap]; 

    // Creates a mutable data object for updating with binary data, like a byte array 
    NSMutableData *pdfData = [NSMutableData data]; 

    // Points the pdf converter to the mutable data object and to the UIView to be converted 
    UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, stringSize.width, stringSize.height), nil); 
    UIGraphicsBeginPDFPage(); 

    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 
    [str drawInRect:CGRectMake(0, 0, stringSize.width, stringSize.height) withFont:[UIFont systemFontOfSize:15]]; 

    // remove PDF rendering context 
    UIGraphicsEndPDFContext(); 

    // Retrieves the document directories from the iOS device 
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

    NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; 

    // instructs the mutable data object to write its context to a file on disk 
    [pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 

    return pdfData; 
} 

編輯:現在你有PDF DataPDF filestoreNSDocument Directory所以print使用UIPrintInteractionController

+0

感謝您的回答 – 2013-05-08 09:41:38

+0

偉大的答案,如果返回的數據傳遞給UIActivityVC它使打印,郵寄和共享PDF文件非常容易。 – EmilDo 2014-05-19 14:58:55