UIActivityViewController如何将字符串转换为print

时间:2012-12-31 15:07:30

标签: objective-c ios6

我正在使用UIActivityViewController从我的应用程序共享一些文本。我可以将文本发送到消息,电子邮件,副本等,但我无法将其发送到打印。

我看到UIActivityTypePrint采用NSData的数据类型。如何创建UIActivityViewController以将我的字符串作为字符串和NSData,以便视图具有可用的打印选项?

NSString *textToShare = self.note.note;
        NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, nil];
        shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
        shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];

2 个答案:

答案 0 :(得分:9)

知道了。它不直接使用NSData,它需要UISimpleTextPrintFormatter。

NSString *textToShare = self.note.note;

UISimpleTextPrintFormatter *printData = [[UISimpleTextPrintFormatter alloc]
                                                         initWithText:textToShare];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare,printData, nil];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];     

答案 1 :(得分:0)

在数组itemsToShare中添加另一项NSData类型

NSString *textToShare = self.note.note;
NSData* data = [textToShare dataUsingEncoding:NSUTF8StringEncoding];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare,data, nil];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];

OR

使用字典发送项目

NSString *textToShare = self.note.note;
NSData* data = [textToShare dataUsingEncoding:NSUTF8StringEncoding];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:textToShare forKey:@"shareString"];
[dictionary setObject:data forKey:@"shareData"];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:dictionary, nil];
[dictionary release];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];