9 Replies Latest reply on Jun 28, 2007 3:05 PM by JabbyPandaUA

    How to draw a string in a rectangle

    aprildavid
      Follwoing are the code:

      var child:Shape = new Shape();
      child.graphics.beginFill(bgColor);
      child.graphics.lineStyle(borderSize, borderColor);
      child.graphics.drawRoundRect(0, 0, size, size, cornerRadius);
      child.graphics.endFill();
      addChild(child);

      How can I draw a string inside the round rectangle?

      Thanks!

      April
        • 1. Re: How to draw a string in a rectangle
          aprildavid Level 1
          Does anyone know how to do it?

          April
          • 2. Re: How to draw a string in a rectangle
            miragevideo
            Yes, sort of. I am doing this, but I first put the shape into an image. Then add the image to a container, like Canvas, then add a Label to theis container. So something like this:

            var a:Canvas = new Canvas();
            var b:Image = new Image();
            var c:Label = new Label();
            var d:Shape;

            b.addChild(d); // add your shape to the image
            a.addChild(b); //add this image to the Canvas
            a.addChild(c); // add the label to the canvas on top of the image

            You may not need the image, but that is how I am doing it.
            • 3. Re: How to draw a string in a rectangle
              JabbyPandaUA Level 3
              Sorry, my english is not my native...

              Does term "string" equals to "line" in English?
              • 4. Re: How to draw a string in a rectangle
                aprildavid Level 1
                Thank you. String doesn't equal to line.
                "asas" is a string. "asas \n" is a line.

                I will try your method and let you know the result.
                • 5. Re: How to draw a string in a rectangle
                  aprildavid Level 1
                  It doesn't work.
                  Following is my code, it only draw 2 rectangles, it didn't draw the rectangle with the tet infor, what is wrong/



                  public class blockChart extends Sprite {
                  private var size:uint = 80;
                  private var bgColor:uint = 0xFFCC00;
                  private var borderColor:uint = 0x666666;
                  private var borderSize:uint = 0;
                  private var cornerRadius:uint = 9;
                  private var gutter:uint = 5;

                  public function blockChart() {
                  doDrawRect();
                  doDrawRect();
                  doDrawTextRect();
                  refreshLayout();
                  }

                  private function refreshLayout():void {
                  var ln:uint = numChildren;
                  var child:DisplayObject;
                  var lastChild:DisplayObject = getChildAt(0);
                  lastChild.x = gutter;
                  lastChild.y = gutter;
                  for (var i:uint = 1; i < ln; i++) {
                  child = getChildAt(i);
                  child.x = gutter + lastChild.x + lastChild.width;
                  child.y = gutter;
                  lastChild = child;
                  }
                  }

                  private function doDrawTextRect():void {
                  var child:Shape = new Shape();
                  var halfSize:uint = Math.round(size / 2);
                  child.graphics.beginFill(bgColor);

                  child.graphics.lineStyle(borderSize, borderColor);
                  child.graphics.drawCircle(halfSize, halfSize, halfSize);
                  child.graphics.endFill();
                  // addChild(child);

                  var a:Canvas = new Canvas();
                  var b:Image = new Image();
                  var c:Label = new Label();
                  c.text="asadf";
                  b.addChild(child); // add your shape to the image
                  a.addChild(b); //add this image to the Canvas
                  a.addChild(c); // add the label to the canvas on top of the image
                  addChild(a);
                  }


                  private function doDrawRect():void {
                  var child:Shape = new Shape();
                  child.graphics.beginFill(bgColor);
                  child.graphics.lineStyle(borderSize, borderColor);
                  child.graphics.drawRect(0, 0, size, size);
                  child.graphics.endFill();
                  addChild(child);
                  }
                  }
                  • 6. Re: How to draw a string in a rectangle
                    aprildavid Level 1
                    Any body knows how to draw a rectangle, and then draw a string on that?
                    • 9. Re: How to draw a string in a rectangle
                      JabbyPandaUA Level 3
                      I can hear you :)

                      So, the string is combination of characters a..zA..Z1..9 and so on, right?

                      If yes, use flash.text.TextField object to display strings http://livedocs.adobe.com/flex/2/langref/flash/text/TextField.html

                      package {
                      import flash.display.Sprite;
                      import flash.text.TextField;
                      import flash.filters.DropShadowFilter;
                      public class HelloWorld extends Sprite {
                      public function HelloWorld() {
                      var shad:DropShadowFilter = new DropShadowFilter(2, 45, 0x000000, 25, 3, 3, 2, 2);
                      var txt:TextField = new TextField();
                      txt.textColor = 0xFFFFFF;
                      txt.filters = [shad];
                      txt.width = 120;
                      txt.x = Math.random()*300;
                      txt.y = Math.random()*300;
                      txt.selectable = false;
                      txt.text = "Hello World welcome! ["+Math.round(txt.x)+","+Math.round(txt.y)+"]";
                      addChild(txt);
                      }
                      }
                      }