- (NSString *)transformToPinyin { NSMutableString *mutableString = [NSMutableString stringWithString:self]; CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false); CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false); return mutableString; }
- (NSString *)transformToPinyin { NSMutableString *mutableString = [NSMutableString stringWithString:self]; BOOL isNeedTransform = ![self isAllEngNumAndSpecialSign]; if (isNeedTransform) { CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false); CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false); } return mutableString; } - (BOOL)isAllEngNumAndSpecialSign { NSString *regularString = @"^[A-Za-z0-9\\p{Z}\\p{P}]+$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regularString]; return [predicate evaluateWithObject:self]; }
第四阶段:参考http://nshipster.com/cfstringtransform/
- (NSString *)transformToPinyin { NSMutableString *mutableString = [NSMutableString stringWithString:self]; BOOL isNeedTransform = ![self isAllEngNumAndSpecialSign]; if (isNeedTransform) { CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false); CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripCombiningMarks, false); } return mutableString; } - (BOOL)isAllEngNumAndSpecialSign { NSString *regularString = @"^[A-Za-z0-9\\p{Z}\\p{P}]+$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regularString]; return [predicate evaluateWithObject:self]; }
- (NSString *)transformToPinyin { NSMutableString *mutableString = [NSMutableString stringWithString:self]; CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false); mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]]; return mutableString; }
Demo移步https://github.com/panghaijiao/PHJTransformPinyinDemo
文章来源:http://www.olinone.com