Unity DF-GUI 中文输入以及自动换行
2014-04-24 18:40
399 查看
在使用Daikon Forge GUI过程中,发现dfTextbox编辑框无法输入中文,dfLabel和dfRichTextLabel文本控件中文无法自动换行。对源代码进行了更改,以便支持中文。不同版本可能更改的地方会不同,这边的版本为Daikon
Forge GUI 1.0.15,Unity 4.3.1。
当前无法输入中文,是因为没有开启Input.imeCompositionMode IME组合方式。打开dfTextbox.cs文件,添加以下方法:
效果图:
文本控件中文无法自动换行,是因为DF-GUI只把英文进行分词,方式是遇到空格就分词,这边要加上中文的处理,即遇到中文就要进行分词。打开dfMarkupTokenizer.cs文件,添加一个扩展方法,用来判断是否中文,代码如下:
修改以下方法:
打开dfDynamicFont.cs文件,修改以下方法:
打开dfMarkupElement.cs文件,修改以下方法:
效果图:
Forge GUI 1.0.15,Unity 4.3.1。
dfTextbox中文输入
当前无法输入中文,是因为没有开启Input.imeCompositionMode IME组合方式。打开dfTextbox.cs文件,添加以下方法:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | protected internal override void OnGotFocus( dfFocusEventArgs args ) { Input.imeCompositionMode = IMECompositionMode.On; base.OnGotFocus( args ); } protected internal override void OnLostFocus( dfFocusEventArgs args ) { base.OnLostFocus( args ); Input.imeCompositionMode = IMECompositionMode.Auto; } |
dfLabel和dfRichTextLabel中文自动换行
文本控件中文无法自动换行,是因为DF-GUI只把英文进行分词,方式是遇到空格就分词,这边要加上中文的处理,即遇到中文就要进行分词。打开dfMarkupTokenizer.cs文件,添加一个扩展方法,用来判断是否中文,代码如下:1 2 3 4 5 6 7 8 9 | public static class CharExtensions { public static bool IsChinese(this Char ch) { var low = '\u4E00'; var high = '\u9FA5'; return ch.CompareTo(low) * ch.CompareTo(high) <= 0; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | public class dfMarkupTokenizer { private dfMarkupToken parseNonWhitespace() { var startOffset = index; var endOffset = index; while( index < source.Length ) { // 中文每个字单独分开 if ( Peek().IsChinese() ) { Advance(); break; } var next = Advance(); if( char.IsWhiteSpace( next ) || AtTagPosition() ) { break; } endOffset += 1; } var token = dfMarkupToken.Obtain( source, dfMarkupTokenType.Text, startOffset, endOffset ); return token; } } public class dfPlainTextTokenizer { private List<dfMarkupToken> tokenize( string source ) { // Flush the object pools dfMarkupToken.Reset(); dfMarkupTokenAttribute.Reset(); tokens.Clear(); var i = 0; var x = 0; var length = source.Length; while( i < length ) { // Skip carriage returns altogether if( source[ i ] == '\r' ) { i += 1; x = i; continue; } #region Extract non-whitespace text while( i < length && !char.IsWhiteSpace( source[ i ] ) ) { // 中文每个字单独分开 if ( source[ i ].IsChinese() ) { i += 1; break; } i += 1; } if( i > x ) { tokens.Add( dfMarkupToken.Obtain( source, dfMarkupTokenType.Text, x, i - 1 ) ); x = i; } #endregion // …… } return this.tokens; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | public class dfDynamicFont : dfFontBase { public class DynamicFontRenderer : dfFontRendererBase { private dfList<LineRenderInfo> calculateLinebreaks() { try { //@Profiler.BeginSample( "Calculate line breaks" ); if( lines != null ) { return lines; } lines = dfList<LineRenderInfo>.Obtain(); var font = (dfDynamicFont)Font; var lastBreak = 0; var startIndex = 0; var index = 0; var lineWidth = 0; var lineHeight = font.Baseline * TextScale; while( index < tokens.Count && lines.Count * lineHeight <= MaxSize.y + lineHeight ) { var token = tokens[ index ]; var type = token.TokenType; if( type == dfMarkupTokenType.Newline ) { lines.Add( LineRenderInfo.Obtain( startIndex, index ) ); startIndex = lastBreak = ++index; lineWidth = 0; continue; } var tokenWidth = Mathf.CeilToInt( token.Width ); var canWrap = WordWrap && lastBreak > startIndex && ( type == dfMarkupTokenType.Text || ( type == dfMarkupTokenType.StartTag && token.Matches( "sprite" ) ) ); if( canWrap && lineWidth + tokenWidth >= MaxSize.x ) { if( lastBreak > startIndex ) { //lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak - 1 ) ); // 中文不要减1,不然会缺字 lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak ) ); startIndex = index = ++lastBreak; lineWidth = 0; } else { lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak - 1 ) ); startIndex = lastBreak = ++index; lineWidth = 0; } continue; } if( type == dfMarkupTokenType.Whitespace ) { lastBreak = index; } // 增加中文换行 else if ( type == dfMarkupTokenType.Text && token.Length == 1 && token.Value[0].IsChinese() ) { lastBreak = index; } lineWidth += tokenWidth; index += 1; } if( startIndex < tokens.Count ) { lines.Add( LineRenderInfo.Obtain( startIndex, tokens.Count - 1 ) ); } for( int i = 0; i < lines.Count; i++ ) { calculateLineSize( lines[ i ] ); } return lines; } finally { //@Profiler.EndSample(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | public class dfMarkupString : dfMarkupElement { internal dfMarkupElement SplitWords() { //@Profiler.BeginSample( "dfMarkupString.SplitWords()" ); var tag = dfMarkupTagSpan.Obtain(); var i = 0; var x = 0; var length = Text.Length; while( i < length ) { #region Words while( i < length && !char.IsWhiteSpace( Text[ i ] ) ) { // 中文每个字单独分开 if ( Text[ i ].IsChinese() ) { i += 1; break; } i += 1; } if( i > x ) { tag.AddChildNode( dfMarkupString.Obtain( Text.Substring( x, i - x ) ) ); x = i; } #endregion // …… } //@Profiler.EndSample(); return tag; } } |
相关文章推荐
- Unity DF-GUI 中文输入以及自动换行
- unity Text文本输入中文空格自动换行
- 为什么HTML中输入中文可自动换行而英文不行?
- Unity UILabel 输入空格自动换行的解决
- textarea输入中文和数字换行解决方法
- 黑马学习笔记——jQuery UI Autocomplete 输入中文不自动完成问题解决方案
- 选用英文字体输入中文自动选用中文字体相关
- Unity基于DFGUI的TreeView设计
- DevExpress cxSpreadSheet 自动换行时中文乱码问题的解决【转】
- 发现DreamWeaver 8.0 简体中文版本 Bug,取消了自动换行,没有效果--[已解决]
- Ubuntu16.04安装搜狗输入法,以及解决不能输入中文的办法
- DES加密解密类 可实现对文件非文件加密,以及自动获取密匙和手动输入
- MYGUI/CEGUI中文输入的问题
- unity在webgl端 输入框无法输入中文和中文显示问题的解决
- MyGUI 3.0 支持中文显示及中文输入
- xhtmlrenderer进行HTML转PDF【解决中文、数字、英文过长自动换行问题】
- UnityGUI:在限定区域中自动滚动文字
- sublime关闭自动打开上次的文件和输入中文
- Android使用WebView加载文字总结之中文乱码、禁止横向滚动、自动换行
- UILable“中文”加上“英文或者其他非中文字符”自动换行的问题