iOS字符串转拼音

- (NSString *)Charactor:(NSString *)aString getFirstCharactor:(BOOL)isGetFirst

{

//转成了可变字符串

NSMutableString *str = [NSMutableString stringWithString:aString];

//先转换为带声调的拼音

CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);

//再转换为不带声调的拼音

CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);

CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);

NSString *pinYin = [str capitalizedString];

//转化为大写拼音

if(isGetFirst)

{

//获取并返回首字母

return [pinYin substringToIndex:1];

}

else

{

return pinYin;

}

}

你可能感兴趣的:(iOS字符串转拼音)