A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: [Flash8] measuring height and width of dynamic text using getColorBoundsRect

  1. #1
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666

    [Flash8] measuring height and width of dynamic text using getColorBoundsRect

    This code measures height and width of dynamic text using getColorBoundsRect:

    Code:
    import flash.display.BitmapData;
    import flash.geom.Rectangle;
    this.createEmptyMovieClip ("mc", 10);
    var my_fmt:TextFormat = new TextFormat ();
    my_fmt.font = "Arial";
    my_fmt.size = 16;
    mc.createTextField ("my_txt", 1, 100, 100, 200, 100);
    mc.my_txt.multiline = true;
    mc.my_txt.wordWrap = true;
    mc.my_txt.type = "input";
    mc.my_txt.text = "Type something here";
    mc.my_txt.setTextFormat (my_fmt);
    this.createEmptyMovieClip ("line1", 2);
    line1.lineStyle (2, 0xff00ff, 100);
    this.createEmptyMovieClip ("line2", 3);
    line2.lineStyle (2, 0x00ffff, 100);
    this.onEnterFrame = function () {
    	var my_bmp:BitmapData = new BitmapData (480, 400);
    	my_bmp.draw (mc);
    	var cb:Rectangle = my_bmp.getColorBoundsRect (0xFFFFFFFF, 0xFFFFFFFF, false);
    	//kill bitmap or run out of memory
    	my_bmp.dispose();
    	//draw lines
    	line1.clear ();
    	line1.lineStyle (2, 0xff00ff, 100);
    	line1.moveTo (cb.x, cb.y);
    	line1.lineTo (cb.x, cb.y + cb.height);
    	line2.clear ();
    	line2.lineStyle (2, 0x00ffff, 100);
    	line2.moveTo (cb.x, cb.y);
    	line2.lineTo (cb.x + cb.width, cb.y);
    };
    /*Message originally posted by tonypa on 09-14-2005 at 05:46 PM:,moved to own thread by tomsamson*/

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    does the code have to be that big. It looks quite ugly. Although i'm really hyped about this bitmap stuff, it looks very hard. Maybe after a few tries i'll get it down pat.
    lather yourself up with soap - soap arcade

  3. #3
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Well, the code creates dynamic text area and draws the lines too. Without those its only 4 lines:

    var my_bmp:BitmapData = new BitmapData (480, 400);
    my_bmp.draw (mc);
    var cb:Rectangle = my_bmp.getColorBoundsRect (0xFFFFFFFF, 0xFFFFFFFF, false);
    my_bmp.dispose();

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    awesome, for some reason, looking at everyones code snippets about flash 8, people seem to be creating everything dynamically, this got me thinking that maybe the bitmap functions only worked with dynamically created instances. like "attachMovie". But thanks for clearing that up Tonypa
    lather yourself up with soap - soap arcade

  5. #5
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,793
    I don't understand the point of this...? I just finished a project where I took in XML data and dynamically built a "page" of content inside a movieClip, using createTextField .

    I think you are trying to address the bug where an MC's width and height property do not get updated when dynamic text resizes inside of it, right? But the workaround I had already found was to use createTextField and then the _width and _height properties of that new textfield DO work!

    So in your example here, why aren't you just using mc.my_txt.text._height to determine the height of the newly created text?

    ???

  6. #6
    better than chuck norris
    Join Date
    Jun 2004
    Location
    West Coast of Michigan
    Posts
    667
    flash 8 is all too complicated

  7. #7
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    Yeah, im with Ray Beez, whats so wrong with ._height? Thats what I use, always works fine.
    Christopher Rhodes
    squarecircleco.

  8. #8
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    But _height gives you height of text field, not actual text.

    _root.createTextField("t",10,0,0,150,20);
    trace(t._height);

    It still have _height of 20 even when there is no text at all.

  9. #9
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    if you set the height of the field to 1, you can write stuff, it will then give you the height of the text.
    Christopher Rhodes
    squarecircleco.

  10. #10
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    You srill cant find size of part of the text inside text field

    Lets say you have text:
    "Flash 8 is released"
    And you have 8 in red, italic and 2 pt smaller then other text. How you find the height and width of that?

  11. #11
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    You cant, but looking at your method, you cant at the moment either...
    Christopher Rhodes
    squarecircleco.

  12. #12
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    You have to modify color values in
    getColorBoundsRect (0xFFFFFFFF, 0xFFFFFFFF, false);
    to get other colors then black vs white.

    I just dont know exactly how to do it yet

  13. #13
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    I had to work this one out for a recent project, there's two height properties for a textfield:
    textfield._height and textfield.textHeight

    there's also:
    textfield._width and textfield.textWidth

    one for the box, and one for the text

    both available from player version 6 ;-) though I only discovered them about three weeks ago.

  14. #14
    curiouser and curiouser LittleRed's Avatar
    Join Date
    Mar 2004
    Location
    Nottingham
    Posts
    335
    lesli_felix - thank you, thank you, thank you - saying about textfield.textHeight has just saved me hours of torment.

  15. #15
    Junior Member
    Join Date
    Feb 2006
    Location
    Canada
    Posts
    8

    CSS with this?

    Can someone post how it would be possible to use this function after css has been applied to the text?

  16. #16
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    I'm not so sure I clearly understand the relevance of accomplishing this. Is this a common wall people hit? I don't see why you'd need to find the height of a word in a block of text?
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  17. #17
    Junior Member
    Join Date
    Feb 2006
    Location
    Canada
    Posts
    8
    it allows you to draw a box around your text that fits tighter than the regular textField (which has a 2pixel gutter space around it)

  18. #18
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    If you know the gutter space will always be 2 pixels, surely there wouldn't be this problem? Methinks that's not the case.

    Plus, I'm pretty sure Flash can't use CSS. Don't see what your problem is.
    http://www.birchlabs.co.uk/
    You know you want to.

  19. #19
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Isn't a gutter of 2px the minimum to NOT have the draw rectangle overlap the text itself?
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  20. #20
    Truimagz.com everfornever's Avatar
    Join Date
    Sep 2006
    Location
    St. Louis
    Posts
    1,306
    you can use CSS with flash, I do alot on my sites so the client can easilier update color patterns when updateing the text via notepad or xml, this helps so they dont need to know html, they juct open the CSS and edit the colors and sizes there.

    As to the hieght of the text thing, I think what this is supposed to solve is when you createempty text field and say import text from notepad you need to know the height of the box, because if say a client inputs only one line or 10 lines, you want the box around to scale to it (I assume with 9 slice as well) so it comes out like it would in an html site, were you could have 3-4 boxes on the left that have a border, and also move up or down the page evenly as more text or less text is used.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center