ngui UIInput输入汉字的那些坑
2017-08-29 20:46
183 查看
项目使用ngui,制作角色创建时,及聊天系统时,难免会输入中文字符。结果发现了很奇妙的情况,即限制的长度6(角色名长度)时,在editor中只能输入三个汉字,但是直接设置值可以显示6个汉字。查了下,发现UIInput中Insert处的代码如下处理:
然而,仍然有喜闻乐见的问题,即,输入时,插入后输入汉字,会使得从插入位置到结尾都选中,查了半天才找到原因。且修改方法如下(pc版本):
//byte[] bytestr; //fixed bug:中文和英文数字算作1个 //#if UNITY_STANDALONE_WIN // bytestr = System.Text.Encoding.GetEncoding("GB2312").GetBytes(mValue); //#else // bytestr = System.Text.Encoding.Default.GetBytes(mValue); //#endif // if (characterLimit > 0 && bytestr.Length > characterLimit) // { // //delete the final char // mValue = mValue.Substring(0, mValue.Length - 1); // while (System.Text.Encoding.Default.GetBytes(mValue).Length > characterLimit) // { // mValue = mValue.Substring(0, mValue.Length - 1); // } // }当条件编译不为UNITY_STANDALONE_WIN(下同)时,即它本身的代码,用default的code(打印出来是gb2312),汉字是2个长度,从而只能输入三个汉字。于是,在Set函数处,如是处理
// byte[] bytestr; // //fixed bug //#if UNITY_STANDALONE_WIN // bytestr=System.Text.Encoding.GetEncoding("GB2312").GetBytes(mValue); //#else // bytestr = System.Text.Encoding.Default.GetBytes(mValue); //#endif // if (characterLimit > 0 && bytestr.Length > characterLimit) // { // //delete the final char // mValue = mValue.Substring(0, mValue.Length - 1); // while (System.Text.Encoding.Default.GetBytes(mValue).Length > characterLimit) // { // mValue = mValue.Substring(0, mValue.Length - 1); // } // } UpdateLabel(); if (notify) ExecuteOnChange();从而,可使得设置的character limit在汉字时是统一的。但是,参考农药的聊天,会发现1个英文数字的长度和1个汉字是相同的。这样想想都有毛病。此外,用Encoding.Default判断长度,会导致打包成pc版本后,Default编码为UTf-8,此时1个汉字的长度是3.对比后,假设最大长度为6,则editor中允许输入3个汉字,打包成win版本呢,是2个汉字。简直是有病啊!于是,仔细思考后,将Insert处和Set处的代码注释掉。此时,1个中文和1个英文数字的长度就是一致了。此处对作者Insert处的代码,蜜汁问号。
然而,仍然有喜闻乐见的问题,即,输入时,插入后输入汉字,会使得从插入位置到结尾都选中,查了半天才找到原因。且修改方法如下(pc版本):
// Append IME composition if (mLastIME != ime) { #if !UNITY_STANDALONE mSelectionEnd = string.IsNullOrEmpty(ime) ? mSelectionStart : mValue.Length + ime.Length; //当输入了中文,input.compositionString有值后,会导致end为字串的结尾...蜜汁问号... #endif mLastIME = ime; UpdateLabel(); ExecuteOnChange(); }
相关文章推荐
- NGUI UIInput屏蔽特殊字符(允许输入汉字)
- j2me 拼音输入法,支持9000多个汉字的,支持"拼音" , "ABC" , "abc" , "特殊符号"四种输入方法
- 只能输入汉字的输入框
- php判断输入是否是纯数字,英文,汉字的方法
- cmd里输入汉字切换输入法没反应解决
- 无法使用输入法输入汉字
- js 限制input只能输入数字、字母和汉字等等
- 非正则实现的只能输入汉字的输入框
- textfield输入中文,未确定具体字时,如何获取输入框中的汉字拼音
- 一种导致UITextView无法输入中文打出汉字的原因
- input 输入框 只能输入数字、字母、汉字等
- 只能输入汉字 和 字母
- 解决控制台输入乱码,用Scanner输入时输入汉字,但是软件读入时出现乱码
- 控制输入框的汉字输入
- 检测input每次的输入是否合法遇到汉字输入就有问题
- 检测input每次的输入是否合法遇到汉字输入就有问题
- SQL Server数据库的表中不能输入汉字的问题
- Ubuntu下搜狗输入法只显示黑框,不显示输入的汉字选项
- AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)2
- Ubuntu 中文输入,编译开发环境,播放阅读,聊天上网 开源的汉字字库