Iphone 如何通过AirPrint打印UITextField文本
我有这个代码来进行文本视图文本的空中打印Iphone 如何通过AirPrint打印UITextField文本,iphone,ios,ios5,airprint,Iphone,Ios,Ios5,Airprint,我有这个代码来进行文本视图文本的空中打印 -(IBAction)_clickbtnprintermainnote:(id)sender { UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController]; print.delegate = self; UIPrintInfo *printInfo = [UIPrintInfo printInfo];
-(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中的默认pinter选项那样获得任何打印机弹出选项。我没有使用条形按钮,我有一个UIbutton。如何正确执行此操作,我的代码中是否有任何错误?\u txtmain注释是我的textview
提前感谢。我得到了解决方案,只需更改这行代码
[print presentAnimated:YES completionHandler:completionHandler]
至[打印presentFromRect:CGRectMake(600650100200)inView:main注释页面视图动画:YES completionHandler:completionHandler]代码>
因为我使用的是iPad应用程序。从中,打印项
:
对象必须是NSURL
、NSData
、UIImage
的实例,或
ALAsset
class
因此,你可能必须通过以下考试:
NSData* data=[_txtmainnote.text dataUsingEncoding:NSUTF8StringEncoding];
print.printingItem = data;
所以上面我的代码是错误的?所以我必须插入你的代码行太正确了?我已经正确地得到了打印弹出窗口,但我无法测试它。请先用真正的AirPrint打印机或打印机模拟器工具尝试使用你的代码,如果不起作用,请尝试使用我的解决方案。你知道@Mat that realy不起作用--它正在寻找PDF文件。看起来没有立即将格式化文本呈现为Airprint的方法。。真糟糕。(当然你可以把它打印成位图…)这看起来很奇怪。它应该可以很好地工作(可能有一个警告)与更简单的代码行在那里;我认为问题与此无关。请解释你的答案
@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];
}