1

Padding left can be done by TextFormat.leftMargin.
And padding right can be done by TextFormat.rightMargin.

But there is no topMargin or bottomMargin property in TextField or TextFormat.
How can I do padding top and bottom?


Example code of left and right padding(margin) of TextField:

var format:TextFormat = new TextFormat format.leftMargin = 40 format.rightMargin = 40 var text:TextField = new TextField text.defaultTextFormat = format text.background = true text.backgroundColor = 0xeeaaaa text.autoSize = TextFieldAutoSize.CENTER text.text = 'abc' 
3
  • Using semicolon is optional, but its a good std. You can use. Commented Sep 6, 2011 at 13:19
  • Sorry, I forgot to add semicolons. I like to omit semicolons at the end of lines. But I'm usually trying to add semicolons in my questions at stackoverflow or in programs at work. Commented Sep 6, 2011 at 14:46
  • use leading property from TextFormat class help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… Commented Jul 16, 2020 at 12:50

4 Answers 4

3

Just increase the height of your sprite, text.height + 10 for example, and set text.y = 5 for a 5 pixel top and bottom margin

Sign up to request clarification or add additional context in comments.

Comments

2

You can also add a blank line before and after your text. Combined with the border and background properties of a textField object you can render nice boxes. Code would look like this:

var text:TextField = new TextField text.defaultTextFormat = format text.background = true text.backgroundColor = 0xeeaaaa text.autoSize = TextFieldAutoSize.CENTER text.text = '\n' + 'your text here' + '\n ' 

If you use this solution, do not forget the blank space after the second blank line. It makes sure the blank line is displayed.

1 Comment

This is a strange world but after reading this great hint I voted up.
1

I believe there's no native way to do it. Here is a list of supported CSS tags (it does not include any padding, nor top or bottom margin): http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/StyleSheet.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6

1 Comment

That's true. AS3 doesn't have a HTML renderer so you can't expect it to have CSS rendering capabilities. All Flash is really doing is emulating SOME of the CSS properties into text field properties for CONVENIENCE - nothing more. It makes it easier to adapt an existing style guide from a website etc. If you need more advanced rendering (like vertical padding) you need to build the functionality yourself. For example, vertical padding is really just pushing all other content down - you need to build that yourself.
1

There is no predefined option available right now.

But you can text.y property instead of top margin and bottom margin.

var format:TextFormat = new TextFormat; format.leftMargin = 40; format.rightMargin = 40; var text:TextField = new TextField; text.defaultTextFormat = format; text.autoSize = TextFieldAutoSize.CENTER; text.text = 'abc\ndef\nhij\nlmno'; var spr:Sprite = new Sprite(); spr.graphics.beginFill(0xeeaaaa,1); spr.graphics.drawRect(0,0, text.width,text.height); spr.graphics.endFill(); addChild(spr); spr.addChild(text); 

2 Comments

Thanks! But text.y just set the position of whole TextField. I want to increase the area between border of TextField and text of TextField. Border can be visible by setting properties, background and backgroundColor or border and borderColor of TextField.
you can use sprite or movieClip for setting bgcolor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.