当添加一次一个字母文本字段,我该如何避免换行存在的中间字? [英] When adding a single letter at a time to a text field, how do I avoid the word wrap occuring mid-word?

查看:241
本文介绍了当添加一次一个字母文本字段,我该如何避免换行存在的中间字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已知以下SWF: 样品

注意如何与在第一行的末尾加上发烧友和写在第二行的末尾,他们开始在第一行,但过了几个字母,他们都碰到打字了。

据我所知,这是正确的行为,但有没有办法为发烧友,开始被输入第二行,第三行,而不是在打字过程中被撞毁?

写入

目前我想做了搜索提前机制的,所以它找到在整个下一个字,然后使该活性字打印,暂时打印,看它是否会增加numlines,并且如果它插入线突破并继续写作。但似乎繁琐。

低于code:

 进口API元素flash.text.TextField;
进口对象类型:flash.events.Event;

变种TT:文本字段=新的TextField();
tt.wordWrap = TRUE;
tt.width = 200;
tt.height = 50;
tt.border = TRUE;

VAR S =堆栈溢出是专业和发烧级的程序员,人谁写的code,因为他们喜欢。我们觉得最好的堆栈溢出的问题,有一点源$ C ​​$ C在其中,但如果你的问题一般包括;
的addChild(TT);

VAR currentLetter:= 0;

的addEventListener(Event.ENTER_FRAME,的OnEnter,假,0,真正的);

功能的OnEnter(五:事件):无效
{
    如果(currentLetter< s.length)
    {
        tt.appendText(s.charAt(currentLetter));
    }
    currentLetter ++;
}
 

解决方案

调整你的codeA位并张贴在wonderfl结果:的 http://wonderfl.net/c/rZkm

作为@mouseas建议我把下一个字成附加的文本框测量的宽度和比较这对在当前行中的剩余空间...如果下一个字不适合我添加一行断并继续。

这里的code:

 包{
    进口flash.geom.Rectangle;
    进口flash.text.TextFieldAutoSize;
    进口flash.display.Sprite;
    进口API元素flash.text.TextField;
    进口对象类型:flash.events.Event;

    公共类FlashTest扩展Sprite {

        私人VAR currentLetter:= 0;
        私人变种TT:文本字段;
        私人VAR调试:文本字段;
        私人变种pre:文本字段;
        私人变种S:字符串;

        公共职能FlashTest()
        {
            //写AS3 code在这里..
            TT =新的TextField();
            tt.wordWrap = TRUE;
            tt.width = 200;
            tt.height = 150;
            tt.border = TRUE;

            S =堆栈溢出是专业和发烧级的程序员,人谁写的code,因为他们喜欢。我们觉得最好的堆栈溢出的问题,有一点源$ C ​​$ C在其中,但如果你的问题一般涉及 ;
            的addChild(TT);

            // predraw字
            pre =新的TextField();
            pre.y = 150;
            pre.width = 200;
            pre.height = 50;
            pre.autoSize = TextFieldAutoSize.LEFT;
            pre.border = TRUE;
            的addChild(pre);

            //调试TXT
            调试=新的TextField();
            debug.x = 250;
            debug.wordWrap = TRUE;
            debug.width = 200;
            debug.height = 500;
            debug.border = TRUE;
            的addChild(调试);

            的addEventListener(Event.ENTER_FRAME,的OnEnter,假,0,真正的);
        }

        私有函数的OnEnter(五:事件):无效
        {

            //debug.appendText("char:+ rect.x ++ rect.width +\ N);
            变种C:字符串=;
            如果(currentLetter< s.length)
            {
                C = s.charAt(currentLetter);
                tt.appendText(C);
            }
            其他
            {
                debug.appendText(DONE \ N!);
                removeEventListener(Event.ENTER_FRAME,的OnEnter);
            }

            如果(C ==)
            {
                VAR RECT:长方形= tt.getCharBoundaries(currentLetter-1);
                如果(RECT!= NULL)
                {
                    //debug.appendText("char:+ RECT +\ N);
                    VAR CPOS:INT = rect.x + rect.width;
                    变种R:INT = tt.width  -  4  -  CPOS; // 4PX的排水沟左+右文本框的



                    VAR启动:INT = s.lastIndexOf(,currentLetter);
                    VAR结束:INT = s.indexOf(,currentLetter + 1);
                    如果(开始℃,)开始= 0;
                    pre.text = s.substr(开始,结束启动);

                    debug.appendText(休息:+ R ++ pre.textWidth +>中+ pre.text +\ N);

                    如果(R  -  pre.textWidth< = 0)
                    {
                        tt.appendText(\ N);
                        debug.appendText(\ N);
                    }

                    //debug.appendText("w:+ tt.textWidth ++启动+ - >中+端+/+ pre.text +/+ pre.textWidth + \ N);
                }
            }

            ++ currentLetter;
        }
    }
}
 

的伟大工程 - 只有一个,在4号线制造麻烦 - 也许你需要微调计算了一下......

Given the following example SWF: Sample

Notice how with the words "enthusiast" at the end of the first line and "write" at the end of the second line, that they start to type out on the first line but after a few letters they are bumped.

I understand this is the correct behavior, but is there a way for "enthusiast" to begin being typed on the second line, and "write" on the third line instead of being bumped during the typing?

Currently I am thinking of doing a search ahead mechanism, so it finds the next word in whole, then makes that the active word to print, temporarily print it, see if it increases the numlines, and if it does insert a line break and continue writing. But it seems fiddly.

Code below:

import flash.text.TextField;
import flash.events.Event;

var tt:TextField = new TextField();
tt.wordWrap = true;
tt.width = 200;
tt.height = 50;
tt.border = true;

var s = "Stack Overflow is for professional and enthusiast programmers, people who write code because they love it. We feel the best Stack Overflow questions have a bit of source code in them, but if your question generally covers";
addChild(tt);

var currentLetter:int = 0;

addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);

function onEnter(e:Event):void 
{
    if(currentLetter < s.length)
    {
        tt.appendText(s.charAt(currentLetter));
    }
    currentLetter++;
}

解决方案

adjusted your code a bit and posted the result on wonderfl: http://wonderfl.net/c/rZkm

as @mouseas suggested i put the next word into an additional textfield measure the width and compare this to the remaining space in the current line ... if the next word doesn't fit I add a line-break and continue.

here's the code:

package {
    import flash.geom.Rectangle;
    import flash.text.TextFieldAutoSize;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.Event;

    public class FlashTest extends Sprite {

        private var currentLetter:int = 0;
        private var tt:TextField;
        private var debug:TextField;
        private var pre:TextField;
        private var s:String;

        public function FlashTest() 
        {
            // write as3 code here..
            tt = new TextField();
            tt.wordWrap = true;
            tt.width = 200;
            tt.height = 150;
            tt.border = true;

            s = "Stack Overflow is for professional and enthusiast programmers, people who write code because they love it. We feel the best Stack Overflow questions have a bit of source code in them, but if your question generally covers";
            addChild(tt);

            // predraw word
            pre = new TextField();
            pre.y = 150;
            pre.width = 200;
            pre.height = 50;
            pre.autoSize = TextFieldAutoSize.LEFT;
            pre.border = true;
            addChild(pre);

            // debug txt
            debug = new TextField();
            debug.x = 250;
            debug.wordWrap = true;
            debug.width = 200;
            debug.height = 500;
            debug.border = true;
            addChild(debug);

            addEventListener(Event.ENTER_FRAME, onEnter, false, 0, true);
        }

        private function onEnter(e:Event):void 
        {

            //debug.appendText("char: " + rect.x + " " + rect.width + "\n");
            var c:String = "";
            if (currentLetter < s.length)
            {
                c = s.charAt(currentLetter);
                tt.appendText(c);
            }
            else
            {
                debug.appendText("DONE! \n");
                removeEventListener(Event.ENTER_FRAME, onEnter);    
            }

            if (c == " ")
            {
                var rect:Rectangle = tt.getCharBoundaries(currentLetter-1);
                if (rect != null)
                {
                    //debug.appendText("char: " + rect + "\n");
                    var cPos:int = rect.x + rect.width;
                    var r:int = tt.width - 4 - cPos;  // 4px for gutter on left+right side of textfield 



                    var start:int = s.lastIndexOf(" ", currentLetter);
                    var end:int = s.indexOf(" ", currentLetter+1);
                    if (start < 0) start = 0;
                    pre.text = s.substr(start, end-start);

                    debug.appendText("rest: " + r + " " + pre.textWidth + " > " + pre.text + "\n");

                    if (r - pre.textWidth <= 0)
                    {
                        tt.appendText("\n");
                        debug.appendText("\n");
                    }

                    //debug.appendText("w:" + tt.textWidth + " " + start + "->"+ end + " /" + pre.text + "/ " + pre.textWidth + "\n");
                }
            }

            ++currentLetter;
        }
    }
}

works great - only the "a" in the 4th line makes trouble - maybe you need to finetune the calculations a bit...

这篇关于当添加一次一个字母文本字段,我该如何避免换行存在的中间字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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