Date
Dec. 22nd, 2024
 
2024年 11月 22日

Post: iOS 小知识: 汉字转拼音

iOS 小知识: 汉字转拼音

Published 12:10 Oct 14, 2014.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

介绍一些 iOS 小知识。

汉字转拼音

@interface NSString (MandarinLatin)
- (NSString *)stringByReplacingMandarinToLatinWithDiacritics:(BOOL)diacritics firstLetterCapitalizes:(BOOL)capitalizes;
@end

@implementation NSString (MandarinLatin)
- (NSString *)stringByReplacingMandarinToLatinWithDiacritics:(BOOL)diacritics firstLetterCapitalizes:(BOOL)capitalizes {
    NSMutableString *original = [NSMutableString stringWithString:self];
    // 转换为带变声符的拼音
    CFStringTransform((CFMutableStringRef)original, NULL, kCFStringTransformMandarinLatin, NO);
    if (!diacritics) {
        // 去除变音符
        CFStringTransform((CFMutableStringRef)original, NULL, kCFStringTransformStripDiacritics, NO);
    }

    if (capitalizes) {
        return [original capitalizedString];
    } else {
        return [original copy];
    }
}
@end
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询