汉字转拼音方法

(多音字无效)

请看如下代码:

NSString *hanziText = @"北京市";
    if ([hanziText length]) {
        NSMutableString *ms = [[NSMutableString alloc] initWithString:hanziText];
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
            NSLog(@"pinyin: %@", ms);
            self.area = ms;
        }
        if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
            NSLog(@"pinyin: %@", ms);
            self.area = ms;
        }  
    }

控制台打印结果:

2016-05-26 11:57:56.372 TianQi[2618:119100] pinyin: běi jīng shì
2016-05-26 11:58:00.196 TianQi[2618:119100] pinyin: bei jing shi

你可能感兴趣的:(汉字转拼音方法)