我在ios中有三个nsmutablearray。我必须使用打印机打印如下例所示的数据,我该怎么做?
例如: -
0.5 1 10
1 10 11
2 5 22
3 4 6
答案 0 :(得分: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];
现在获取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
使用{{1}}
答案 1 :(得分:0)
使用以下代码进行遍历和打印。
for (int i=0; i<[array count]; i++) {
NSLog(@"%@",[array objectAtIndex:i]);
}
答案 2 :(得分: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了解详情。