[英]Use localized unit of appropriate magnitude in iOS 10
我在iOS 10中使用新的Measurement API來獲取長度測量的本地化字符串:
let length = Measurement(value: 200, unit: UnitLength.centimeters)
let str = MeasurementFormatter().string(from: length)
但是,對於我的(英國)語言環境,我然后返回"0.001 mi"
,這對顯示字符串不是很有幫助。 相反,我更喜歡使用一個使用適當單位(例如"6.56 feet"
或"2.19 yards"
)的字符串作為測量值。 使用API可以做到這一點嗎?還是我最好使用帶有SI前綴的自定義解決方案?
解決方案很簡單:
let length = Measurement(value: 200, unit: UnitLength.centimeters)
let f = MeasurementFormatter()
f.unitOptions = MeasurementFormatter.UnitOptions.naturalScale
f.numberFormatter.maximumFractionDigits = 2
let str = MeasurementFormatter().string(from: length)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.