diff --git a/flixel/text/FlxText.hx b/flixel/text/FlxText.hx index 1075598a4b..ad4099d580 100644 --- a/flixel/text/FlxText.hx +++ b/flixel/text/FlxText.hx @@ -35,11 +35,16 @@ import openfl.utils.AssetType; /** * Extends FlxSprite to support rendering text. Can tint, fade, rotate and scale just like a sprite. Doesn't really animate * though. Also does nice pixel-perfect centering on pixel fonts as long as they are only one-liners. + * + * ## Autosizing + * + * By default `FlxText` is autosized to fit it's text. + * To set a fixed size, * use the `fieldWidth`, `fieldHeight` and `autoSize` fields. */ class FlxText extends FlxSprite { /** - * 2px gutter on both top and bottom + * 4px gutter at the bottom when the field has automatic height */ static inline var VERTICAL_GUTTER:Int = 4; @@ -122,14 +127,27 @@ class FlxText extends FlxSprite /** * The width of the `TextField` object used for bitmap generation for this `FlxText` object. * Use it when you want to change the visible width of text. Enables `autoSize` if `<= 0`. + * + * **NOTE:** auto width always implies auto height */ public var fieldWidth(get, set):Float; /** - * Whether the `fieldWidth` should be determined automatically. Requires `wordWrap` to be `false`. + * The height of `TextField` object used for bitmap generation for this `FlxText` object. + * Use it when you want to change the visible height of the text. Enables "auto height" if `<= 0`. + * + * **NOTE:** Fixed height has no effect if `autoSize = true`. + */ + public var fieldHeight(get, set):Float; + + /** + * Whether the `fieldWidth` and `fieldHeight` should be determined automatically. + * Requires `wordWrap` to be `false`. */ public var autoSize(get, set):Bool; + var _autoHeight:Bool = true; + /** * Offset that is applied to the shadow border style, if active. * `x` and `y` are multiplied by `borderSize`. Default is `(1, 1)`, or lower-right corner. @@ -527,6 +545,8 @@ class FlxText extends FlxSprite { wordWrap = false; autoSize = true; + // auto width always implies auto height + _autoHeight = true; } else { @@ -544,6 +564,29 @@ class FlxText extends FlxSprite return (textField != null) ? textField.width : 0; } + function get_fieldHeight():Float + { + return (textField != null) ? textField.height : 0; + } + + function set_fieldHeight(value:Float):Float + { + if (textField == null) + return value; + + if (value <= 0) + { + _autoHeight = true; + } + else + { + _autoHeight = false; + textField.height = value; + } + _regen = true; + return value; + } + function set_autoSize(value:Bool):Bool { if (textField != null) @@ -786,8 +829,10 @@ class FlxText extends FlxSprite } var newWidth:Int = Math.ceil(textField.width); + var textfieldHeight = _autoHeight ? textField.textHeight : textField.height; + var vertGutter = _autoHeight ? VERTICAL_GUTTER : 0; // Account for gutter - var newHeight:Int = Math.ceil(textField.textHeight) + VERTICAL_GUTTER; + var newHeight:Int = Math.ceil(textfieldHeight) + vertGutter; // prevent text height from shrinking on flash if text == "" if (textField.textHeight == 0) @@ -798,14 +843,15 @@ class FlxText extends FlxSprite if (oldWidth != newWidth || oldHeight != newHeight) { // Need to generate a new buffer to store the text graphic - height = newHeight; var key:String = FlxG.bitmap.getUniqueKey("text"); makeGraphic(newWidth, newHeight, FlxColor.TRANSPARENT, false, key); if (_hasBorderAlpha) _borderPixels = graphic.bitmap.clone(); - frameHeight = newHeight; - textField.height = height * 1.2; + + if (_autoHeight) + textField.height = newHeight; + _flashRect.x = 0; _flashRect.y = 0; _flashRect.width = newWidth;