关于文本框标点符号避头尾的问题

你好,我有一个FGUI文本框的问题,就是在FGUI的编辑器的状态下。

文本框里的文本是避头尾的,也就是标点符号不会出现在每行的第一个字符,这个是我想要的




编辑器.png




 
然而当发布到Unity的编辑器下时,同样的内容分辨率的情况下,文本框里的文本却不再遵守这个规则了,这是我不想要的,我想要的就是标点符号不能出现在每行第一个字符上。
 



TIM图片20180912184515.png


 
请问这个问题有没有办法避免,还是说有其他的解决方案啊。
 
我的FGUI的编辑器是3.9.5 我的unity编辑器是5.6.4,如果能解决的话,万分感谢了。
已邀请:

NaoNaoNao

赞同来自: 谷主

自己看TextField.cs源码改的BuildLines,就加了一小段,可以试试
 private readonly string strRegex = @"(\!|\?|\,|\。|\《|\》|\(|\)|\(|\)|\:|\“|\‘|\、|\;|\+|\-|\·|\#|\¥|\;|\”|\【|\】|\——|\/)";
 
 
void BuildLines2()
        {
            float letterSpacing = _textFormat.letterSpacing * _fontSizeScale;
            float lineSpacing = (_textFormat.lineSpacing - 1) * _fontSizeScale;
            float rectWidth = _contentRect.width - GUTTER_X * 2;
            float glyphWidth = 0, glyphHeight = 0, baseline = 0;
            short wordLen = 0;
            bool wordPossible = false;
            float posx = 0;

            TextFormat format = _textFormat;
            _font.SetFormat(format, _fontSizeScale);
            bool wrap = _wordWrap && !_singleLine;
            if (_maxWidth > 0)
            {
                wrap = true;
                rectWidth = _maxWidth - GUTTER_X * 2;
            }
            _textWidth = _textHeight = 0;

            RequestText();

            int elementCount = _elements.Count;
            int elementIndex = 0;
            HtmlElement element = null;
            if (elementCount > 0)
                element = _elements[elementIndex];
            int textLength = _parsedText.Length;

            LineInfo line = LineInfo.Borrow();
            _lines.Add(line);
            line.y = line.y2 = GUTTER_Y;
            sLineChars.Clear();

            
            for (int charIndex = 0; charIndex < textLength; charIndex++)
            {
                char ch = _parsedText[charIndex];
                glyphWidth = glyphHeight = baseline = 0;

                while (element != null && element.charIndex == charIndex)
                {
                    if (element.type == HtmlElementType.Text)
                    {
                        format = element.format;
                        _font.SetFormat(format, _fontSizeScale);
                    }
                    else
                    {
                        IHtmlObject htmlObject = element.htmlObject;
                        if (_richTextField != null && htmlObject == null)
                        {
                            element.space = (int)(rectWidth - line.width - 4);
                            htmlObject = _richTextField.htmlPageContext.CreateObject(_richTextField, element);
                            element.htmlObject = htmlObject;
                        }
                        if (htmlObject != null)
                        {
                            glyphWidth = htmlObject.width + 2;
                            glyphHeight = htmlObject.height;
                            baseline = glyphHeight * IMAGE_BASELINE;
                        }

                        if (element.isEntity)
                            ch = '\0'; //indicate it is a place holder
                    }

                    elementIndex++;
                    if (elementIndex < elementCount)
                        element = _elements[elementIndex];
                    else
                        element = null;
                }

                if (ch == '\0' || ch == '\n')
                {
                    wordPossible = false;
                }
                else if (_font.GetGlyph(ch == '\t' ? ' ' : ch, out glyphWidth, out glyphHeight, out baseline))
                {
                    if (ch == '\t')
                        glyphWidth *= 4;

                    if (wordPossible)
                    {
                        if (char.IsWhiteSpace(ch))
                        {
                            wordLen = 0;
                        }
                        else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'
                            || ch >= '0' && ch <= '9'
                            || ch == '.' || ch == '"' || ch == '\''
                            || format.specialStyle == TextFormat.SpecialStyle.Subscript
                            || format.specialStyle == TextFormat.SpecialStyle.Superscript
                            || _textDirection != RTLSupport.DirectionType.UNKNOW && RTLSupport.IsArabicLetter(ch))
                        {
                            wordLen++;
                        }
                        else
                            wordPossible = false;
                    }
                    else if (char.IsWhiteSpace(ch))
                    {
                        wordLen = 0;
                        wordPossible = true;
                    }
                    else if (format.specialStyle == TextFormat.SpecialStyle.Subscript
                        || format.specialStyle == TextFormat.SpecialStyle.Superscript)
                    {
                        if (sLineChars.Count > 0)
                        {
                            wordLen = 2; //避免上标和下标折到下一行
                            wordPossible = true;
                        }
                    }
                    else
                        wordPossible = false;
                }
                else
                    wordPossible = false;

                sLineChars.Add(new LineCharInfo() { width = glyphWidth, height = glyphHeight, baseline = baseline });
                if (glyphWidth != 0)
                {
                    if (posx != 0)
                        posx += letterSpacing;
                    posx += glyphWidth;
                }

                if (ch == '\n' && !_singleLine)
                {
                    UpdateLineInfo(line, letterSpacing, sLineChars.Count);

                    LineInfo newLine = LineInfo.Borrow();
                    _lines.Add(newLine);
                    newLine.y = line.y + (line.height + lineSpacing);
                    if (newLine.y < GUTTER_Y) //lineSpacing maybe negative
                        newLine.y = GUTTER_Y;
                    newLine.y2 = newLine.y;
                    newLine.charIndex = line.charIndex + line.charCount;

                    sLineChars.Clear();
                    wordPossible = false;
                    posx = 0;
                    line = newLine;
                }
                else if (wrap && posx > rectWidth)
                {
                    int lineCharCount = sLineChars.Count;
                    int toMoveChars;

                    if (wordPossible && wordLen < 20 && lineCharCount > 2) //if word had broken, move word to new line
                    {
                        toMoveChars = wordLen;
                        //we caculate the line width WITHOUT the tailing space
                        UpdateLineInfo(line, letterSpacing, lineCharCount - (toMoveChars + 1));
                        line.charCount++; //but keep it in this line.
                    }
                    else
                    {
                        toMoveChars = lineCharCount > 1 ? 1 : 0; //if only one char here, we cant move it to new line
                        UpdateLineInfo(line, letterSpacing, lineCharCount - toMoveChars);
                    }

                    LineInfo newLine = LineInfo.Borrow();
                    _lines.Add(newLine);
                    newLine.y = line.y + (line.height + lineSpacing);
                    if (newLine.y < GUTTER_Y)
                        newLine.y = GUTTER_Y;
                    newLine.y2 = newLine.y;
                    newLine.charIndex = line.charIndex + line.charCount;

                    posx = 0;
                    if (toMoveChars != 0)
                    {
                        for (int i = line.charCount; i < lineCharCount; i++)
                        {
                            LineCharInfo ci = sLineChars[i];
                            if (posx != 0)
                                posx += letterSpacing;
                            posx += ci.width;
                        }

                        sLineChars.RemoveRange(0, line.charCount);
                    }
                    else
                        sLineChars.Clear();

                    wordPossible = false;

                    //line赋值前判断 下一个line的首字符是否为符号,如果是则加入line,然后才切换到下一个new line
                    if (System.Text.RegularExpressions.Regex.IsMatch(_parsedText[charIndex].ToString(), strRegex) && charIndex<textLength )
                    {
                        line.charCount--;
                        newLine.charIndex--;
                        charIndex--;
                    }


                    line = newLine;
                }
            }

            UpdateLineInfo(line, letterSpacing, sLineChars.Count);

            if (_textWidth > 0)
                _textWidth += GUTTER_X * 2;
            _textHeight = line.y + line.height + GUTTER_Y;

            _textWidth = Mathf.RoundToInt(_textWidth);
            _textHeight = Mathf.RoundToInt(_textHeight);
        }
 
 
 
 
 

谷主

赞同来自:

Unity里没有做这种处理。

要回复问题请先登录注册