简体   繁体   中英

How to print an UITextView iOS 8 objective C

I was trying to print through AirPrint one UITextView that the user fills, kind of a note block. That declaration below is the UITextField I want to print:

    IBOutlet UITextView *texto;

And this is my printing code:

    -(IBAction)imprimir:(id)sender{
UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];
print.delegate = self;
NSData* datos=[texto.text dataUsingEncoding:NSUTF8StringEncoding];
print.printingItem = datos;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName =@"Mis Notas";
printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;
print.printingItem = texto.text;

void (^completionHandler)(UIPrintInteractionController *,BOOL, NSError *) = ^(UIPrintInteractionController *print,BOOL completed, NSError *error) {
    if (!completed && error) {
        NSLog(@"No se pudo imprimir");
    }
};
[print presentAnimated:NO completionHandler:completionHandler];
}

Xcode also says me an error with print.delegate = self; as if I need another type of ViewController, I really don´t know. The sources I found use a normal ViewController. The error is: "Assigning to 'id<UIPrintInteractionControllerDelegate<' from incompatible type 'ViewController *const_strong' ". Thank you!

 if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
UIPrintInteractionController *print = [UIPrintInteractionController    sharedPrintController];
print.delegate = self;
NSData* datos=[_label.text dataUsingEncoding:NSUTF8StringEncoding];
print.printingItem = datos;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName =@"Mis Notas";
printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;
print.printingItem = _label.text;

void (^completionHandler)(UIPrintInteractionController *,BOOL, NSError *) = ^(UIPrintInteractionController *print,BOOL completed, NSError *error) {
    if (!completed && error) {
        NSLog(@"No se pudo imprimir");
    }
};
[print presentAnimated:NO completionHandler:completionHandler];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM