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

UIGraphicsBeginPDFContextToFile correct page size based on locale

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

    UIGraphicsBeginPDFContextToFile(pdfURL().path, CGRectZero, nil)

默认情况下创建 pdf ANSI A (612.0, 792.0) 这对美国和加拿大(+一些其他国家)是可以的,但对于其他国家它应该是 ISO216 A4 (595,842)

基本上我需要这个的 iOS 版本 :

NSPrintInfo.sharedPrintInfo().paperSize

我是不是遗漏了什么或者 iOS 上的唯一方法是手动检查语言环境?

解法:

由于美国信函 (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
    //        }

}

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

ISO 3166-2 != ISO 4217

您可能需要更新 ANSI_A_Countries 以匹配货币 ISO。 例如玻利维亚,玻利维亚诺国家 ((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
}
let bestPaper = UIPrintPaper.bestPaper(forPageSize: CGSize.zero, withPapersFrom: [])

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

美国:612 x 792,即US Letter

英国:595.2756 x 841.8898,即 A4

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