在我的应用中,我会生成pdf(可以打印或稍后发送) 我无法找到确定纸张大小的正确方法:
UIGraphicsBeginPDFContextToFile(pdfURL().path, CGRectZero, nil)
默认情况下会创建pdf ANSI A(612.0,792.0),这对美国和美国来说是可以的。加拿大(+更多国家),但其余的应该是 ISO216 A4(595,842)
基本上我需要iOS版本:
NSPrintInfo.sharedPrintInfo().paperSize
我是否遗漏了某些内容,或者iOS的唯一方法是手动检查语言环境?
答案 0 :(得分:5)
由于美国信(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
}
答案 1 :(得分:4)
let bestPaper = UIPrintPaper.bestPaper(forPageSize: CGSize.zero, withPapersFrom: [])
检查bestPaper.paperSize.width
和bestPaper.paperSize.height
,您将获得基于当前区域的本地化结果:
美国:612 x 792
,即美国字母
英国:595.2756 x 841.8898
,即A4
请注意,并没有说明指定空白页面大小和空白纸张列表会生成此默认值,但这是我所看到的。