字符串生成拼音

 

#import "NSString+Pinyin.h"

@implementation NSString (Pinyin)

- (NSString *)generatePinyin {
    
    NSMutableString *ms = [[NSMutableString alloc] initWithString:self];
    /*
    if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
        NSLog(@"Pingying: %@", ms); // wǒ shì zhōng guó rén
    }*/
    
    if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
        //NSLog(@"Pingying: %@", ms); // wo shi zhong guo ren
        return ms;
    }
    
    return nil;
}

- (NSString *)generatePinyinFirstLetter
{
    NSMutableString *ms = [[NSMutableString alloc] initWithString:self];
    
    if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
        //DLog(@"%@", [ms substringFromIndex:1]);
        return [[ms substringToIndex:1] uppercaseString];
    }
    
    /*
    NSString *pinyin = [self generatePinyin];
    DLog(@"pinyin : ")
    if (pinyin) {
        return [pinyin substringToIndex:1];
    }*/
    
    return nil;
    
}

 

 

你可能感兴趣的:(字符串)