-
Yes we can
[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*/
-
M.D.
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.
-
Senior Member
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();
-
M.D.
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
-
Senior Member
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?
???
-
flash 8 is all too complicated
-
Heli Attack!
Yeah, im with Ray Beez, whats so wrong with ._height? Thats what I use, always works fine.
-
Senior Member
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.
-
Heli Attack!
if you set the height of the field to 1, you can write stuff, it will then give you the height of the text.
-
Senior Member
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?
-
Heli Attack!
You cant, but looking at your method, you cant at the moment either...
-
Senior Member
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
-
....he's amazing!!!
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.
-
curiouser and curiouser
lesli_felix - thank you, thank you, thank you - saying about textfield.textHeight has just saved me hours of torment.
-
CSS with this?
Can someone post how it would be possible to use this function after css has been applied to the text?
-
Pumpkin Carving 2008
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
-
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)
-
Script kiddie
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.
-
Pumpkin Carving 2008
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
-
Truimagz.com
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|