iOS 4:如何模拟A4打印机? [英] iOS 4: How do I simulate an A4 printer?

查看:310
本文介绍了iOS 4:如何模拟A4打印机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS应用中实现打印。我在Mac上有一个打印代理应用程序,用于物理测试打印机,这是一台美国Letter打印机。 iOS模拟器附带的打印机模拟器也默认为US Letter。



任何人都有关于如何测试其他默认纸张尺寸的建议吗?



谢谢!

解决方案

如果我理解了iOS打印功能 - 这是一个很大的 if - 然后打印模拟器将根据您发送的尺寸调整为US Letter或A4。因此,如果您发送A4大小的东西,它将选择A4,如果您发送美国信件大小的东西,它将选择美国信。



但是,我发现实际印刷,这并不总是有效。为了做到这一点,我发现它明显鼓励iOS选择A4纸张尺寸。这是在UIPrintInteractionController委托中的printInteractionController:choosePaper:方法中完成的(参见下面的代码)。如果那里有人更好地理解这一点,请发布。



更普遍(并且有点问题......)我采取的打印方法是美国信或A4的应用程序设置。当应用程序首次启动时,它会检查它是否是美国(通过测试[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]),如果是,则选择US Letter。否则它是A4。用户可以更改此设置。 (我让用户导出PDF,因此,如果她是英国用户向美国朋友发送PDF,她可以让他们看起来正确。)



要打印,我首先创建A4或US Letter PDF。 Apple的示例代码将引导您完成美国信函大小。对于A4,您需要将PDF的尺寸设置为大约595.4 x 841.7。 (对于使用其他大小,请注意这些数字是以点为单位,其中点密度为72dpi,即您以英寸为单位并乘以72.)然后在UIPrintInteractionController上使用setPrintingItem:并为PDF提供NSURL。 / p>

我发现iOS打印文档非常具有挑战性,因此可以使用更简单/更好/更强大的方法来处理A4 / US Letter等。但希望我在这里写的一些内容是有帮助的。



委托代码

  // UIPrintInteractionController委托中的代码
// [共享设置]对象返回paperWidth和paperHeight,具体取决于app wide A4或US Letter设置
- (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList
{
CGSize pageSize = CGSizeMake([[设置共享] paperWidth],[[设置共享] paperHeight]);
return [UIPrintPaper bestPaperForPageSize:pageSize withPapersFromArray:paperList];
}

创建PDF代码

  UIGraphicsBeginPDFContextToFile(CGRectMake(0.0,0.0,[[设置共享] paperWidth],[[设置共享] paperHeight]),无;); 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0,0.0,[[设置共享] paperWidth],[[设置共享] paperHeight]),nil);

//创建PDF

UIGraphicsEndPDFContext();


I'm implementing printing in an iOS app. I have a print proxy app on a Mac for a physical test printer, which is a US Letter printer. The Printer Simulator that ships with the iOS simulator also appears to default to US Letter.

Anyone have suggestions on how I can test for other default paper sizes?

Thanks!

解决方案

If I've understood the iOS printing features aright -- and that's a big if - then the Print Simulator will adjust to US Letter or A4 depending on the dimensions of what you send. So, if you send it something A4 sized it will select A4 and if you send it something US Letter sized, it will select US Letter.

However, I found with actual printing, this didn't always work. To get that right, I found it made a different to explicitly encourage iOS to select an A4 paper size. This is done in the printInteractionController:choosePaper: method in the UIPrintInteractionController delegate (see code below). If anyone out there understands this better, please post.

More generally (and going off question a bit...) the approach I've taken to printing is to have an app setting for US Letter or A4. When the app first starts up it checks whether it is the US (by testing [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode]) and if so, chooses US Letter. Otherwise it's A4. The user can change this. (I let the user export PDFs, so, say, if she's a UK user sending PDFs to US pals, she can make them look right.)

To print, I first create an A4 or US Letter PDF. Apple's sample code walks you through this for the US Letter size. For A4 you need to set the dimensions of the PDF to around 595.4 x 841.7. (For working with other sizes, note that these numbers are in points, where point density is 72dpi, i.e. you take the size in inches and multiply by 72.) I then use setPrintingItem: on the UIPrintInteractionController with an NSURL for the PDF.

I have found the iOS printing documentation quite challenging, so it's not unlikely that a simpler/better/more robust approach to tackling A4/US Letter etc. is available. But hopefully, some of what I've written here is of help.

Delegate code

// code in the UIPrintInteractionController delegate
// The [Shared settings] object returns paperWidth and paperHeight depending on the app wide A4 or US Letter setting
- (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList
{
    CGSize pageSize = CGSizeMake([[Settings shared] paperWidth], [[Settings shared] paperHeight]);
    return [UIPrintPaper bestPaperForPageSize:pageSize withPapersFromArray:paperList];
}

Creating PDF code

UIGraphicsBeginPDFContextToFile(CGRectMake(0.0, 0.0, [[Settings shared] paperWidth], [[Settings shared] paperHeight]), nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, [[Settings shared] paperWidth], [[Settings shared] paperHeight]), nil);

// create the PDF

UIGraphicsEndPDFContext();

这篇关于iOS 4:如何模拟A4打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆