As far as we know that in English speaking country, people always write their names down as format "FirstName MiddleName LastName", such as "Steven Paul Jobs". However in some other language, it is different, for example, in China, people usually write name as "LastName FirstName" format. I want to find out something in "NSLocale Class Reference" but it did not help me a lot. I know that I can do "switch case" by NSLocaleCountryCode, but I do not know every country's habit. So, is there any better way to solve the name order issues? Thanks for help.
3 Answers
This functionality has been added in Foundation.framework. Compatible with iOS9+.
I do not see it in the documentation yet but it is in the header files and you can use it in a Swift Playground.
The details of this problem are covered in WWDC 2015 Session 227 (about 22 minutes in). In this session they talk about an NSPersonNameComponents and NSPersonNameComponentsFormatter.
Example:
let components = NSPersonNameComponents()
components.namePrefix = "Mrs."
components.givenName = "Grace"
components.middleName = "Murray"
components.familyName = "Hopper"
NSPersonNameComponentsFormatter.localizedStringFromPersonNameComponents(
components, style: .Default, options: [])

- 799
- 5
- 18
-
seems there is no way to set a custom locale, but it alway uses the system set :| – danielemm Mar 11 '20 at 15:37
Yes, there's a kit for that written by Mattt who developed Afnetworking:
TTTNameFormatter
TTTNameFormatter formats names according to the internationalization standards of the AddressBook framework, which determine, for example, the display order of names and whether or not to delimit components with whitespace.
TTTNameFormatter is not available on OS X.
Example Usage
TTTNameFormatter *nameFormatter = [[TTTNameFormatter alloc] init];
NSString *frenchName = [nameFormatter stringFromPrefix:nil firstName:@"Guillaume" middleName:@"François" lastName:@"Antoine" suffix:@"Marquis de l'Hôpital"];
NSLog(@"%@", frenchName);
// "Guillaume François Antoine Marquis de l'Hôpital"
NSString *japaneseName = [nameFormatter stringFromFirstName:@"孝和" lastName:@"関"];
NSLog(@"%@", japaneseName);
// "関孝和"
get it here:

- 4,615
- 2
- 19
- 36
You can use the AddressBook framework to get the name order. Just add it to the project and do #import "AddressBookUI/AddressBookUI.h"
, and then you can use this method, for example:
- (NSString*)localNameFormatterWithFirstName:(NSString*)firstName middleName:(NSString*)middleName andLastName:(NSString*)lastName {
ABRecordRef record = ABPersonCreate();
ABRecordSetValue(record, kABPersonFirstNameProperty, (__bridge CFStringRef)firstName, NULL);
ABRecordSetValue(record, kABPersonMiddleNameProperty, (__bridge CFStringRef)middleName, NULL);
ABRecordSetValue(record, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);
NSString *displayName = (__bridge_transfer NSString*)ABRecordCopyCompositeName(record);
CFRelease(record);
return displayName;
}
Any argument that is nil will be ignored.

- 6,356
- 7
- 32
- 47
-
Thank you very much. TTTNameFormatter is also based on AddressBookUI, it really works. (However, I heard that, AddressBookUI will be deprecated in iOS9. It seem that I have to write another If/Else about iOS version.) – Jihang Yang Aug 31 '15 at 02:19