- (instancetype)combineDiacritics{ static NSDictionary<NSNumber *,NSNumber *> *sDiacriticalSubstDict; //unichar of diacritic -> unichar of combining diacritic static dispatch_once_t oncetoken; dispatch_once(&oncetoken,^{ //http://www.unicode.org/charts/pdf/U0300.pdf sDiacriticalSubstDict = @{ @(0x02cb) : @(0x0300),@(0x00b4) : @(0x0301),@(0x02c6) : @(0x0302),@(0x02dc) : @(0x0303),@(0x02c9) : @(0x0304),//Grave,Acute,Circumflex,Tilde,Macron @(0x00af) : @(0x0305),@(0x02d8) : @(0x0306),@(0x02d9) : @(0x0307),@(0x00a8) : @(0x0308),@(0x02c0) : @(0x0309),//Overline,Breve,Dot above,Diaeresis @(0x00b0) : @(0x030a),@(0x02da) : @(0x030b),@(0x02c7) : @(0x030c),@(0x02c8) : @(0x030d),@(0x02bb) : @(0x0312),//Ring above,Double Acute,Caron,Vertical line above,Cedilla above @(0x02bc) : @(0x0313),@(0x02bd) : @(0x0314),@(0x02b2) : @(0x0321),@(0x02d4) : @(0x0323),@(0x02b1) : @(0x0324),//Comma above,Reversed comma above,Palatalized hook below,Dot below,Diaeresis below @(0x00b8) : @(0x0327),@(0x02db) : @(0x0328),@(0x02cc) : @(0x0329),@(0x02b7) : @(0x032b),@(0x02cd) : @(0x0331),//Cedilla,Ogonek,Vert line below,Inverted double arch below,Macron below }; }); NSMutableString* __block buffer = [NSMutableString stringWithCapacity:self.length]; [self enumerateSubstringsInRange:NSMakeRange(0,self.length) options:NsstringEnumerationByComposedCharacterSequences usingBlock: ^(Nsstring* substring,NSRange substringRange,NSRange enclosingRange,BOol* stop) { Nsstring *newString = nil; if (substring.length == 1) //The diacriticals are all Unicode BMP. { unichar uniChar = [substring characteratIndex:0]; unichar newUniChar = [sDiacriticalSubstDict[@(uniChar)] integerValue]; if (newUniChar != 0) { NSLog(@"Unichar %04x => %04x",uniChar,newUniChar); newString = [Nsstring stringWithCharacters:&newUniChar length:1]; } } if (newString) [buffer appendString:newString]; else [buffer appendString:substring]; }]; Nsstring *precomposedStr = [buffer precomposedStringWithCanonicalmapPing]; return precomposedStr;}
有没有人知道更多内置的方式来进行这种转换?
解决方法 没有内置方法来执行此转换,因为间距修改器字母块(U 02B0..U 02FF)中的字符不打算用作变音符号.从Unicode标准的7.8节开始:They are not formally combining marks (gc=Mn or gc=Mc) and do not graphically combine with the base letter that they modify. They are base characters in their own right.
Spacing Clones of Diacritics. Some corporate standards explicitly specify spacing and nonspacing forms of combining diacritical marks,and the Unicode Standard provIDes matching codes for these interpretations when practical.
如果要将它们转换为组合表单,则需要从Spacing Modifier Letters code chart中的交叉引用构建一个表(就像您已经在做的那样).
总结以上是内存溢出为你收集整理的objective-c – 如何在iOS上转换为“组合变音符号”全部内容,希望文章能够帮你解决objective-c – 如何在iOS上转换为“组合变音符号”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)