IOS: Get the first letter of Pinyin using swift, and get the first letter of every word in a sentence, iosswift
In a recent project, I encountered the problem of getting the first letter of Pinyin. I searched for the online method and did not find a suitable and useful method. So I did some research and wrote the following methods. Please feel free to contact me, I hope to help you.
/// PYFirst. swift // get the first letter of Pinyin, support to get the first letter of each word in a sentence // Created by Zhou Mi (mrzhou@miw.cn) on 2015/6/1 (Children's Day ). // Copyright (c) 2015 www.miw.cn. all rights reserved. // import Foundationfunc PYFirst (string: String ?, _ AllFirst: Bool = false)-> String {var py = "#" if let s = string {if s = "" {return py} var str = CFStringCreateMutableCopy (nil, 0, s) CFStringTransform (str, nil, kCFStringTransformToLatin, Boolean (0) CFStringTransform (str, nil, kCFStringTransformStripCombiningMarks, Boolean (0 )) py = "" if allFirst {for x in (str as String ). componentsSeparatedByString ("") {py + = PYFirst (x)} else {py = (str As NSString). substringToIndex (1). uppercaseString} return py}/* // call example, return # var s: String? PYFirst (s) s = "People's Republic of China @ hi wor \ r \ nld. "// call example, return ZHRMGHGWPYFirst (s, true) // call example, return ZPYFirst (s )*/
This may be the best gift for Children's Day.