PersonNameComponents.FormatStyle

This is part of the FormatStyle Deep Dive series

The PersonNameComponents.FormatStyle handles the localized display of a person’s various name components.


Download the Xcode Playground with all examples

See the examples as a gist


Since the order of display for names are varied between countries, languages, and cultures. It’s important to get it right.

If given a chance, storing a person’s name as PersonNameComponents will alleviate a lot of hassle in the future in regards to localization. It’s untenable to manually support this.

let guest = PersonNameComponents(
    namePrefix: "Dr",
    givenName: "Elizabeth",
    middleName: "Jillian",
    familyName: "Smith",
    nameSuffix: "Esq.",
    nickname: "Liza"
)

guest.formatted() // "Elizabeth Smith"

guest.formatted(.name(style: .abbreviated)) // "ES"
guest.formatted(.name(style: .short))       // "Liza"
guest.formatted(.name(style: .medium))      // "Elizabeth Smith"
guest.formatted(.name(style: .long))        // "Dr Elizabeth Jillian Smith Esq."

Setting the locale is achieved by using the .locale() method:

let chinaLocale = Locale(identifier: "zh_CN")

guest.formatted(.name(style: .abbreviated).locale(chinaLocale))    // "SE"
guest.formatted(.name(style: .short).locale(chinaLocale))          // "Liza"
guest.formatted(.name(style: .medium).locale(chinaLocale))         // "Smith Elizabeth"
guest.formatted(.name(style: .long).locale(chinaLocale))           // "Dr Smith Elizabeth Jillian Esq."

Note: You can see that the variants all respect the order of the given and family names.

To fully customize the style, you follow the same conventions as other format styles:

let inFrance = PersonNameComponents.FormatStyle(style: .long, locale: Locale(identifier: "fr_FR"))

inFrance.format(guest)
guest.formatted(inFrance)

Attributed String Output

AttributedString output can be had by this formatter by adding the .attributed parameter to the FormatStyle during use.

You can read more details in the AttributedString Output deep-dive


Download the Xcode Playground with all examples

See the examples as a gist


Tags: