如何通过AirPrint打印UITextField文本

时间:2012-05-16 10:31:11

标签: iphone ios ios5 airprint

我正在使用此代码将AirPrint作为textview文本

-(IBAction)_clickbtnprintermainnote:(id)sender
{
    UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

    print.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName =@"Nots of MyBibleApp";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    print.printInfo = printInfo;
    print.showsPageRange = YES;
    print.printingItem = _txtmainnote.text;

    void (^completionHandler)(UIPrintInteractionController *,BOOL, NSError *) = ^(UIPrintInteractionController *print,BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"houston we have a problem");
        }
    };
    [print presentAnimated:YES completionHandler:completionHandler];
}

但是我没有得到任何打印机选项的弹出窗口,比如iPad.am中的默认pinter选项没有使用条形按钮,我有一个UIbutton.how来正确地执行此操作,我的代码中是否有任何错误? ._txtmainnote是我的文本视图。

提前致谢。

3 个答案:

答案 0 :(得分:3)

来自thisprintingItem

  

该对象必须是NSURLNSDataUIImage的实例,或者   ALAsset上课。

所以你可能要传递类似的内容:

NSData* data=[_txtmainnote.text dataUsingEncoding:NSUTF8StringEncoding];
print.printingItem = data;

答案 1 :(得分:0)

我得到了解决方案,只需将此行代码[print presentAnimated:YES completionHandler:completionHandler];更改为[print presentFromRect:CGRectMake(600, 650, 100, 200) inView:mainnotepageview animated:YES completionHandler:completionHandler]; 因为我正在使用iPad应用程序。

答案 2 :(得分:0)

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIView *mainnotepageview;
@property (strong, nonatomic) IBOutlet UITextField *text02;
@property (strong, nonatomic) IBOutlet UITextView *myTextView;

 - (IBAction)btnSettingsTapped:(id)sender
{
 NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@,  %@",self.myTextView.text, self.label.text];
//[printBody appendFormat:@"\n\n\n\nPrinted From *myapp*"];

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = self.label.text;
pic.printInfo = printInfo;

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
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);
    }
};

//[pic presentFromBarButtonItem:self.rightButton animated:YES completionHandler:completionHandler];
[pic presentFromRect:CGRectMake(600, 650, 100, 200) inView:_mainnotepageview animated:YES completionHandler:completionHandler];

}