How to print a UITextField text by AirPrint

1.7k Views Asked by At

i am having this code to do the AirPrint the textview text

-(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];
}

but i didn't get any popup for printer option like the default pinter option in iPad.am not using a bar button,i have a UIbutton.how to do this correctly,is there any mistake in my code?._txtmainnote is my textview.

Thanks in advance.

3

There are 3 best solutions below

3
On BEST ANSWER

From this, the printingItem:

The object must be an instance of the NSURL, NSData, UIImage, or ALAsset class.

so probably you have to pass something like:

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

i got the solution,,just change this line of code [print presentAnimated:YES completionHandler:completionHandler]; to [print presentFromRect:CGRectMake(600, 650, 100, 200) inView:mainnotepageview animated:YES completionHandler:completionHandler]; because am using iPad app.

1
On
@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];

}