Flash - 在TLFTextField上设置最大行数

时间:2013-03-06 18:42:49

标签: actionscript-3 flash tlf

可以设置TLFTextField上显示的最大行数吗?所以我有一个文本,而不是我想在整个文本中显示那个,只有3个第一行可见。我该如何配置?

这就是我所拥有的:

         var myTLFTextField:TLFTextField = new TLFTextField();
         addChild(myTLFTextField); 
         myTLFTextField.x = 0;
         myTLFTextField.y = 0;
         myTLFTextField.width = _width
         myTLFTextField.height = 100;
         myTLFTextField.multiline = true;
         myTLFTextField.wordWrap = true;


         var myFormat:TextLayoutFormat = new TextLayoutFormat();
         myFormat.color = 0x336633;
         myFormat.fontFamily = "Arial, Helvetica, _sans";
         myFormat.fontSize = 24;
         myFormat.textAlign = TextAlign.LEFT;

        var textFlow:TextFlow = myTLFTextField.textFlow;
        var p:ParagraphElement = new ParagraphElement();
        var span1:SpanElement = new SpanElement();
        var span2:SpanElement = new SpanElement();
        var inlineGraphicElement:InlineGraphicElement = new InlineGraphicElement();
        var textLayoutFormat:TextLayoutFormat = new TextLayoutFormat();

        //Add graph
        inlineGraphicElement.source = drwCircle();
        inlineGraphicElement.float = Float.LEFT;

        //Add Text to the spans
        span1.text = "You can draw a happy face here ";
        span2.text = "if you like.as asdfads ad fas fadsf f asdfsdf asd sdafas dff asd adsf adsf adsf asf sadf asdf dfghjf  j  fhj fgffg hgfhj fg fgj fg jkb asdljk ljka jlkj asdjfh lajsdfh sd sdf asdfasd fdas asd fa sdfadsf asd adsf ad fadsf adsf ads fads fads f adsf asdf ";
        p.fontSize = 16;
        p.addChild(inlineGraphicElement);
        p.addChild(span1);
        p.addChild(span2);


        // Add Paragraph to text flow and update controller to display
        textFlow.addChild(p);
        textFlow.hostFormat = myFormat;
        textFlow.flowComposer.updateAllControllers();

2 个答案:

答案 0 :(得分:1)

幸运的是,使用TLFTextfield我们有paddingToppaddingBottom的文字以及总textHeight像素的属性。知道文本字段中文本的总numLines,我们可以计算高度的像素数我们想要显示的行数。

编辑:我注意到,对于文本字段的某些 smalllish 宽度,numLines属性不符合预期值...我不知道这是否可能是一个BUG,但在计算之前阅读它似乎可以解决它。

试试这个(在所有代码之后加上这些行):

//Pre read numLines property :(
myTLFTextField.numLines;

//how many lines you want to show
var numLines:uint = 3;

//set textfield height to the proportion between the total lines of text and
//the number of lines to show, taking into account the paddings of text
myTLFTextField.height = myTLFTextField.paddingTop + 
                        myTLFTextField.paddingBottom + 
                        (myTLFTextField.textHeight*(numLines+1)/myTLFTextField.numLines);

希望这有帮助!

答案 1 :(得分:0)

您可以使用其他剪辑屏蔽该区域以隐藏溢出的文本。