Cocoa method to return a list of all letters in the current locale's alphabet?
I'm currently trying to implement a UITableView
with a delegate and data source that replicates, as closely as possible, the functionality found the ABAddressBookUI
framework's ABPeoplePickerNavigationController
class, but with the added ability to select multiple contacts at once (as indicated by adding/removing an accessory view to the appropriate UITableViewCell
).
Everything is working fine, with the exception of providing localized "section index titles" (the letters that appear in the scroll overlay to the right of the screen) for the UITableView
, as should be returned by the data source method:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
Obviously, I could just return an NSArray
containing the NSStrings
A, B, C ...Z, but I'd ideally like this method to return an array of all of the (uppercase, where applicable) letters of the alphabet of the current locale.
One promising lead was:
[[
NSLocale
currentLocale ] objectForKey:@"NSLocaleExemplarCharacterSet" ]
But I can't find any significant documentation on this and it returns an NSCharacterSet
, from which I've been unable to extract the actual characters (if I could do that, then NSCharacterSet's capitalizedLetterCharacterSet
could also be a promising starting point).
I've also run an otool -tV
on the AddressBookUI
framework, which revealed a call to the function ABAddressBookCopySectionIndices()
, into which an ABAddressBookRef
can be passed to obtain exactly what I'm looking for… a CFArray of the localized alphabet. However, it's a private function, so I can't use it in my app.
So, does anybody know whether Cocoa Touch supports this functionality? And, if not, are there any ideas as to how ABAddressBookCopyIndices()
is working its magic? I suspect the International Components for Unicode library may well hold the key, but I'm not (as y开发者_开发百科et) familiar with its capabilities...
Please don't assume that the section indices should be "capital letters". That's only valid for some European languages. E.g. Japanese or Chinese don't have the concept of capital letters, they just have too many letters to start with, but there's a customary set of labels usually used in real-world dictionaries or real-world address books.
Instead, use the standard UILocalizedIndexedCollation. It gives exactly what you need.
Got bored and wrote this up for you
NSArray * charactersInCharacterSet(NSCharacterSet *charSet)
{
NSMutableArray * array = [NSMutableArray array];
NSData * data = [charSet bitmapRepresentation];
const char* bytes = [data bytes];
for (int i = 0; i < 8192; ++i)
{
if (bytes[i >> 3] & (((unsigned int)1) << (i & 7)))
{
[array addObject:[NSString stringWithFormat:@"%C", i]];
}
}
return [NSArray arrayWithArray:array];
}
NSCharacterSet * charSet = [[[[NSLocale alloc] initWithLocaleIdentifier:@"sr_Cyrl_ME"] autorelease] objectForKey:NSLocaleExemplarCharacterSet];
NSArray * chars = charactersInCharacterSet(charSet);
for (NSString *str in chars)
{
NSLog(@"%@", str);
}
That will give you an array of the characters in the set.
this one help you to get what exactly you want
let currentCollation = UILocalizedIndexedCollation.current() as UILocalizedIndexedCollation
print(currentCollation)
精彩评论