UIGraphicsBeginPDFContextToFile根据区域设置确定正确的页面大小

Cry*_*ppo 3 printing pdf ios swift

在我的应用程序中,我生成pdf(可以打印或稍后发送),我找不到确定纸张大小的正确方法:

    UIGraphicsBeginPDFContextToFile(pdfURL().path, CGRectZero, nil)
Run Code Online (Sandbox Code Playgroud)

默认情况下创建pdf ANSI A(612.0,792.0),对美国和加拿大(+更多国家/地区)可以,但其余的应该是ISO216 A4(595,842)

基本上我需要iOS版本:

NSPrintInfo.sharedPrintInfo().paperSize
Run Code Online (Sandbox Code Playgroud)

我错过了什么或iOS上的唯一方法是手动检查区域设置?

Cry*_*ppo 6

方案:

由于美国信(ANSI A)仅用于北美,玻利维亚,哥伦比亚,巴拿马,委内瑞拉,菲律宾,哥斯达黎加和智利.我们可以从当前的区域设置中获取国家/地区代码,并检查它们是否在这些国家/地区的列表中.

SWIFT 3(扩展):

extension NSLocale {

    struct StandardPageDimensions  {
        static let ANSI_A        = CGSize(width:612,height:792)
        static let ANSI_B        = CGSize(width:792,height:1224)
        static let ANSI_C        = CGSize(width:1584,height:1224)
        static let ANSI_D        = CGSize(width:2448,height:1584)
        static let ANSI_E        = CGSize(width:3168,height:2448)

        static let ISO216_A0     = CGSize(width:2384,height:3370)
        static let ISO216_A1     = CGSize(width:1684,height:2384)
        static let ISO216_A2     = CGSize(width:1190,height:1684)
        static let ISO216_A3     = CGSize(width:842,height:1190)
        static let ISO216_A4     = CGSize(width:595,height:842)
        static let ISO216_A5     = CGSize(width:420,height:595)
        static let ISO216_A6     = CGSize(width:298,height:420)
        static let ISO216_A7     = CGSize(width:210,height:298)
        static let ISO216_A8     = CGSize(width:148,height:210)
    }

    static let ANSI_A_Countries : Set<String> = ["US","CA","MX","CU","DO","GT","CR","SV","HN","BO","CO","VE","PH","CL"]

    //If you want to use Locale use regionCode
    static func defaultPaperSize()->CGSize {
        let locale = self.current
        return ANSI_A_Countries.contains( locale.regionCode ?? "" ) ? StandardPageDimensions.ANSI_A : StandardPageDimensions.ISO216_A4
    }

    //otherwise cast to NSLocale
    //        static func defaultPaperSize()->CGSize {
    //            let locale = self.current as NSLocale
    //            return ANSI_A_Countries.contains( locale.object(forKey: NSLocale.Key.countryCode) as! String ) ? StandardPageDimensions.ANSI_A : StandardPageDimensions.ISO216_A4
    //        }

}
Run Code Online (Sandbox Code Playgroud)

我没有使用Locale.currencyCode的原因是因为CurrencyCode并不总是等于CountryCode.

ISO 3166-2!= ISO 4217

您可能需要更新ANSI_A_Countries以匹配货币ISO.例如玻利维亚,Bolíviano国家((https://en.wikipedia.org/wiki/ISO_3166-2:BO))ISO代码被指定为BO但货币代码仅具有BOL.

SWIFT 2:

struct StandardPageDimensions  {
    static let ANSI_A        = CGSizeMake(612,792)
    static let ANSI_B        = CGSizeMake(792,1224)
    static let ANSI_C        = CGSizeMake(1584,1224)
    static let ANSI_D        = CGSizeMake(2448,1584)
    static let ANSI_E        = CGSizeMake(3168,2448)

    static let ISO216_A0     = CGSizeMake(2384,3370)
    static let ISO216_A1     = CGSizeMake(1684,2384)
    static let ISO216_A2     = CGSizeMake(1190,1684)
    static let ISO216_A3     = CGSizeMake(842,1190)
    static let ISO216_A4     = CGSizeMake(595,842)
    static let ISO216_A5     = CGSizeMake(420,595)
    static let ISO216_A6     = CGSizeMake(298,420)
    static let ISO216_A7     = CGSizeMake(210,298)
    static let ISO216_A8     = CGSizeMake(148,210)
}

let ANSI_A_Countries : Set<String> = ["US","CA","MX","CU","DO","GT","CR","SV","HN","BO","CO","VE","PH","CL"]

func defaultSizeForCurrentLocale()->CGSize {
    let locale = NSLocale.currentLocale()
    return ANSI_A_Countries.contains( locale.objectForKey(NSLocaleCountryCode) as! String ) ? StandardPageDimensions.ANSI_A : StandardPageDimensions.ISO216_A4
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*nek 5

let bestPaper = UIPrintPaper.bestPaper(forPageSize: CGSize.zero, withPapersFrom: [])
Run Code Online (Sandbox Code Playgroud)

检查bestPaper.paperSize.width&bestPaper.paperSize.height,您将获得基于当前区域的本地化结果:

美国: 612 x 792,这是美国信函

英国: 595.2756 x 841.8898,即 A4

请注意,没有记录指定空页面大小和空纸张列表将生成此默认值,但这是我看到的行为。