A real drawText call in Flex? - apache-flex

I am now developing a Flex application in which I need to control each pixel of my control of Flex. I want to calculate that how width and how height of some text used in my control and do some layout stuff.After some searching I find that the only way to draw text in Flex is to use something like TextField. So, I use TextField to display text and try to get the width and height of the text through:
textfiled.getLineMetrics(0).width/.height;
But the real textfield is much more bigger than this, so I do:
textfield.width = textfiled.getLineMetrics(0).width;
textfield.height = textfiled.getLineMetrics(0).height;
But, the textfield get part not displayed, and I am surprised by this effect. I know there should be a 2-pixel gutter around the text, but what the remain space? Why nearly 20% part of the text height/width are not displayed?
And how can I get a real drawText call in Flex, I mean something like Windows's drawText method...
Thanks!

you can try this : getCharBoundaries () i've used it and seems accurate hoever it is for char and not a whole text so you will need to iterate through the text
also see this :
Is there a way to get the actual bounding box of a glyph in ActionScript?

Please try:
textfield.width = textfield.textWidth + 4;
textfield.height = textfield.textHeight + 4;
I'm not familiar with Windows's drawText method so I cannot help you there, but perhaps you want to have a look at the stellar text layout functionality in Flex 4.

Related

QTextEdit display width vs text width

I'm creating a 'scrolling-text' class in Qt, using a QTextEdit (read-only, no scrollbars, moveCursor) and a QTimer - simple and working.
My problem is when the text sent to the class is shorter (narrower) than the QTextEdit-box.
Silly, I agree, but, being new to Qt, I didn't find an easy way to compare the width of the given text (depending on the font) and the actual width that can be displayed inside the QTextEdit (after calculating the FrameStyle, etc.). I presume I need to calculate the pixels.
Any ideas?
Thanks
You can get the width of a text using QFontMetrics:
int textWidth = myTextEdit->fontMetrics().width(myTextEdit->text());

How do I truncate a Flex StyleableTextField after two lines?

In my Flex 4.5 mobile app, I have an actionScript item renderer (that derives from Flex's LabelItemRenderer). I want to fit in exactly 2 lines of text, and then truncate the rest. The width and height of the label are fixed and known statically.
How can I do this? The StyleableTextField.truncateToFit() method only works for one line of text.
I've set wordWrap = true, so the text now flows into the second line - but I need to truncate the text if it doesn't fit in two lines.
I need it to show all the text if there is only one line. (in both cases the label should be vertically middle-aligned in my renderer)
I know how to override layoutContents to do sizing and positioning etc of the StyleableTextField. So I'm looking specifically for ideas to implement custom text truncation with the StyleableTextField).
Any ideas?
Unless you're using something specific to the StyleableTextField try the s:Label. It has a property maxDisplayedLines which you could set to 2 and it will handle the truncation.

AS3: grouping sprites

I have a handful of sprites that I am attempting to group together via addChild().
Here is some pseudo-code demonstrating what I would like to accomplish:
import nav.text.TextSprite;
spr1:Sprite = new Sprite();
spr1.graphics.clear();
spr1.graphics.beginFill(0x000000);
spr1.graphics.drawRect(0,0,100,100);
txt1:TextSprite = new TextSprite;
txt1.text = "hello";
spr1.addChild(txt1);
//this is what isn't working: the sprite is hidden but not the text
spr1.alpha = 0.0;
For some reason I cannot seem to get the TextSprite to draw correctly... All it is is a Sprite with a TextField added to it. I think everything is working there, but I might have something wrong w/r/t making sure all of TextSprites children are grouped correctly.
I should mention that it does position correctly; but the alpha property won't respond in the way I would expect it to. I.E., the sprite that the TextField is attached to will allow it's alpha to be set but the text remains visible.
Any thoughts?
Most likely you just need to embed the font in your textfield. Try changing the x, y of spr1 and see if txt1 moves along with it. If it truly is a child then it will respond to the new position.
You need to embed the font using textfield.embedFonts = true. If your text is disappearing when you do this, how are you going about embedding the font (using the Flex embed meta tag or using the Flash IDE?), check that you are not changing the font weight (setting the text to bold when you have only embedded the normal weight font) and if you are using a text format, be sure to apply the text format AFTER you set the textfield.text property. You can get around this by using textfield.defaultTextFormat.

how to wordwrap in flex tree node

I have a Tree object in my Flex code. It's width is 100%, and it is contained by a Panel with width=200. I am using a custom itemrenderer that extends TreeItemRenderer.
I need the text in each node to word wrap if it's too big (as happens often). I have tried
label.wordWrap = true;
in my item renderer without any luck. Any other suggestions?
If 'label' is an instance of the Label component, you won't be able to get it to wrap, no matter what you do, as the Label component doesn't support wrapping. Or, rather, is meant to be used only for single-line text.
If you are using a Label component, try using a Text component, instead. It's meant to display multi-line text.
With the default TreeItemRenderer you just need to set two properties on your Tree to enable wordWrap:
tree.variableRowHeight = true;
tree.wordWrap = true;
Enjoy.
What is the height that is set on the label? Is there enough room for the text to go vertically?
try setting the label width to a constant width. If your not able to do this because it needs to be dynamic then you need to override the update functions and set the width of the label then. The problem is the label hasn't calculated the actual 100% width at the time of rendering so it doesnt know it has to warp or not.
Dont Use Label. Use ' mx:Text width=" 100%" ' ( Text with 100% width) and it will wrap on its own.

How can I float a movieclip over a textarea to make it appear like it's part of the text?

Take a look at this demo, how do they accomplish the Inline Changes feature? To me it looks like they are floating a movieclip over the textarea.
What I can't figure out is how they anchored the movieclip to stay in the correct position. If you type something before the movieclip it moves position along with the text, the movieclips even move to the next line when the text word wraps. Does anyone have an idea?
What I think they're doing is keeping track of the index the sprite is representing. Then they have a nice function that moves the sprite to the correct position given the size of the textbox and font. All you need to do after that is bind the positioning function to changes made to the textbox and you've got that effect. Doesn't seem too complicated, as there are already font size/position functions available.
EDIT: Notice how you cannot type code in between the overlapping regions.

Resources