DistanceFormatter
open class DistanceFormatter : Formatter, NSSecureCoding
A formatter that provides localized representations of distance units and measurements.
This class is limited to UnitLength
and its behavior is more specific to distances than MeasurementFormatter
. By default, the class automatically localizes and rounds the measurement using Measurement.localized(into:)
and Locale.nationalizedCurrent
. Measurements can be formatted into either strings or attributed strings.
-
Declaration
Swift
public static var supportsSecureCoding: Bool
-
Options for choosing and formatting the unit.
Seealso
MeasurementFormatter.unitOptions
Declaration
Swift
open var unitOptions: MeasurementFormatter.UnitOptions { get set }
-
The unit style.
Seealso
MeasurementFormatter.unitStyle
Declaration
Swift
open var unitStyle: Formatter.UnitStyle { get set }
-
The locale that determines the chosen unit, name of the unit, and number formatting.
Seealso
MeasurementFormatter.locale
Declaration
Swift
open var locale: Locale { get set }
-
The underlying measurement formatter.
Declaration
Swift
@NSCopying open var measurementFormatter: MeasurementFormatter
-
Declaration
Swift
public override init()
-
Declaration
Swift
public required init?(coder decoder: NSCoder)
-
Creates and returns a localized, formatted string representation of the given distance in meters.
The distance is converted from meters to the most appropriate unit based on the locale and quantity.
Seealso
MeasurementFormatter.string(from:)
Declaration
Swift
open func string(from distance: CLLocationDistance) -> String
Parameters
distance
The distance, measured in meters, to localize and format.
Return Value
A localized, formatted representation of the distance.
-
Creates and returns a localized, formatted attributed string representation of the given distance in meters.
The distance is converted from meters to the most appropriate unit based on the locale and quantity.
NSAttributedString.Key.quantity
is applied to the range representing the quantity. For example, the “5” in “5 km” has a quantity attribute set to 5.Declaration
Swift
open func attributedString(from distance: CLLocationDistance, defaultAttributes attributes: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString
Parameters
distance
The distance, measured in meters, to localize and format.
defaultAttributes
The default attributes to apply to the resulting attributed string.
Return Value
A localized, formatted representation of the distance.
-
Creates and returns a localized, formatted string representation of the given measurement.
Seealso
MeasurementFormatter.string(from:)
Declaration
Swift
open func string(from measurement: Measurement<UnitLength>) -> String
Parameters
measurement
The measurement to localize and format.
Return Value
A localized, formatted representation of the measurement.
-
Creates and returns a localized, formatted attributed string representation of the given measurement.
NSAttributedString.Key.quantity
is applied to the range representing the quantity. For example, the “5” in “5 km” has a quantity attribute set to 5.Declaration
Swift
open func attributedString(from measurement: Measurement<UnitLength>, defaultAttributes attributes: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString
Parameters
measurement
The measurement to localize and format.
defaultAttributes
The default attributes to apply to the resulting attributed string.
Return Value
A localized, formatted representation of the measurement.
-
Declaration
Swift
open override func string(for obj: Any?) -> String?
-
Declaration
Swift
open override func attributedString(for obj: Any, withDefaultAttributes attrs: [NSAttributedString.Key : Any]? = nil) -> NSAttributedString?