当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 汉字的拼音

iOS 汉字的拼音

2019年07月24日  | 移动技术网移动技术  | 我要评论

废话不多说,直接给大家贴关键代码了。

具体代码如下所示:

#import <foundation/foundation.h>
@interface nsstring (utils)
/**
* 汉字的拼音
*
* @return 拼音
*/
- (nsstring *)pinyin;
@end
#import "nsstring+utils.h"
@implementation nsstring (utils)
//汉字的拼音
- (nsstring *)pinyin{
nsmutablestring *str = [self mutablecopy];
cfstringtransform(( cfmutablestringref)str, null, kcfstringtransformmandarinlatin, no);
cfstringtransform((cfmutablestringref)str, null, kcfstringtransformstripdiacritics, no);
return [str stringbyreplacingoccurrencesofstring:@" " withstring:@""];
}
@end

下面接着看下ios将汉字转成拼音

在ios开发中经常碰到做通讯录需要将汉字转成拼音的情况,以下就是我把汉字转成拼音的方法

+ (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];
}

用kcfstringtransformmandarinlatin方法转化出来的是带音标的拼音,如果需要去掉音标,则继续使用kcfstringtransformstripcombiningmarks方法即可。

以上所述是小编给大家介绍的ios 汉字的拼音,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网