在Swift中使用Unicode代码点 [英] Working with Unicode code points in Swift

查看:193
本文介绍了在Swift中使用Unicode代码点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您对蒙古语的细节不感兴趣,但只想快速回答关于在Swift中使用和转换Unicode值,请跳到和 Character 或应使用 NSString unichar



>



此问题的更新已隐藏以清理页面。查看编辑纪录。

解决方案

字符串和字符



对于将来访问此问题的每个人, String Character 将是你的答案。



直接在代码中设置Unicode值

  var string:String =我想访问北京,Москва,मुंबई,القاهرة和서울시。

If you are not interested in the details of Mongolian but just want a quick answer about using and converting Unicode values in Swift, then skip down to the first part of the accepted answer.


Background

I want to render Unicode text for traditional Mongolian to be used in iOS apps. The better and long term solution is to use an AAT smart font that would render this complex script. (Such fonts do exist but their license does not allow modification and non-personal use.) However, since I have never made a font, let alone all of the rendering logic for an AAT font, I just plan to do the rendering myself in Swift for now. Maybe at some later date I can learn to make a smart font.

Externally I will use Unicode text, but internally (for display in a UITextView) I will convert the Unicode to individual glyphs that are stored in a dumb font (coded with Unicode PUA values). So my rendering engine needs to convert Mongolian Unicode values (range: U+1820 to U+1842) to glyph values stored in the PUA (range: U+E360 to U+E5CF). Anyway, this is my plan since it is what I did in Java in the past, but maybe I need to change my whole way of thinking.

Example

The following image shows su written twice in Mongolian using two different forms for the letter u (in red). (Mongolian is written vertically with letters being connected like cursive letters in English.)

In Unicode these two strings would be expressed as

var suForm1: String = "\u{1830}\u{1826}"
var suForm2: String = "\u{1830}\u{1826}\u{180B}"

The Free Variation Selector (U+180B) in suForm2 is recognized (correctly) by Swift String to be a unit with the u (U+1826) that precedes it. It is considered by Swift to be a single character, an extended grapheme cluster. However, for the purposes of doing the rendering myself, I need to differentiate u (U+1826) and FVS1 (U+180B) as two distinct UTF-16 code points.

For internal display purposes, I would convert the above Unicode strings to the following rendered glyph strings:

suForm1 = "\u{E46F}\u{E3BA}" 
suForm2 = "\u{E46F}\u{E3BB}"

Question

I have been playing around with Swift String and Character. There are a lot of convenient things about them, but since in my particular case I deal exclusively with UTF-16 code units, I wonder if I should be using the old NSString rather than Swift's String. I realize that I can use String.utf16 to get UTF-16 code points, but the conversion back to String isn't very nice.

Would it be better to stick with String and Character or should I use NSString and unichar?

What I have read

Updates to this question have been hidden in order to clean the page up. See the edit history.

解决方案

String and Character

For almost everyone in the future who visits this question, String and Character will be the answer for you.

Set Unicode values directly in code:

var string: String = "I want to visit 北京, Москва, मुंबई, القاهرة, and 서울시. 
                        

这篇关于在Swift中使用Unicode代码点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆