在ios中使用打印机打印nsmutable阵列
我在ios中有三个nsmutable阵列。我必须使用打印机以下例子打印数据,我应该怎么做?在ios中使用打印机打印nsmutable阵列
例如: -
0.5 1 10
1 10 11
2 5 22
3 4 6
您可以使用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。
使用下面的代码来遍历并打印。
for (int i=0; i<[array count]; i++) {
NSLog(@"%@",[array objectAtIndex:i]);
}
不,他问如何打印三个阵列与他在他的问题中表明的格式相同。而你的答案描述了如何打印数组。 – 2013-05-08 08:00:34
对于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];
现在得到所有data
在NSString
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
方法来获取NSData
到print
。
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 Data
和PDF file
也store
在NSDocument Directory
所以print
使用UIPrintInteractionController
感谢您的回答 – 2013-05-08 09:41:38
伟大的答案,如果返回的数据传递给UIActivityVC它使打印,邮寄和共享PDF文件非常容易。 – EmilDo 2014-05-19 14:58:55
你甚至_try_? [绘图和印刷指南的iOS:打印](http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/Printing/Printing.html) – Kreiri 2013-05-08 07:52:12