SoFunction
Updated on 2025-04-03

Pinyin of iOS Chinese characters

Without further ado, I will post the key code to you.

The specific code is as follows:

#import <Foundation/>
@interface NSString (Utils)
/**
 * Pinyin of Chinese characters
 *
 * @return Pinyin
 */
- (NSString *)pinyin;
@end
#import "NSString+"
@implementation NSString (Utils)
//The pinyin of Chinese characters- (NSString *)pinyin{
NSMutableString *str = [self mutableCopy];
CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
return [str stringByReplacingOccurrencesOfString:@" " withString:@""];
}
@end

Let's continue to see iOS convert Chinese characters into pinyin

In iOS development, I often encounter situations where I need to convert Chinese characters into pinyin to make address books. The following is how I convert Chinese characters into pinyin

+ (NSString *)transform:(NSString *)chinese
{
  NSMutableString *pinyin = [chinese mutableCopy];
  CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
  CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
  NSLog(@"%@", pinyin);
  return [pinyin uppercaseString];
}

The pinyin with phonetic symbols is converted using the kCFStringTransformMandarinLatin method. If the phonetic symbols need to be removed, continue to use the kCFStringTransformStripCombiningMarks method.

The above is the pinyin of the iOS Chinese characters introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!