Material Box

Material Box

WEB Design & Material Images

Godot - Trimming Text Based on Button Size

Godot

This GDScript code demonstrates how to trim text based on the size of a Button node.


By default, Button nodes automatically adjust their size to fit the text or icon they contain.

You can enable text trimming based on the button's size by setting `set_clip_text()` to `true` and enabling the `Clip Text` property.

To apply the `Clip Text` property, it must be executed before the Button's size is specified.

# Create a Button node
var button = Button.new()
add_child(button)
button.set_text("This is a Button")

# Enable Clip Text
button.set_clip_text(true)

# Specify the Button's size
button.set_size(Vector2(100, 50))

When `Clip Text` is enabled, the overflowing text is cut off by default, which corresponds to an `OverrunBehavior` property value of `0`.

You can change the trimming method by using `set_text_overrun_behavior()` to specify the `OverrunBehavior` property instead of `Clip Text`.

0: Text is not trimmed. (Default)
1: Trims text character by character.
2: Trims text word by word.
3: Trims text character by character and appends an ellipsis.
4: Trims text word by word and appends an ellipsis.

The `OverrunBehavior` setting must also be executed before the Button's size is specified.

# Create a Button node
var button = Button.new()
add_child(button)
button.set_text("This is a Button")

# Specify OverrunBehavior
button.set_text_overrun_behavior(3)

# Specify the Button's size
button.set_size(Vector2(100, 50))

Godot

TitleGodot - Trimming Text Based on Button Size

CategoryGodot

Created

Update

AuthorYousuke.U