"Property: clip_text When this property is enabled,text that is too large to fit the button is clipped, when disabled the Button will always be wide enough to hold the text."
Looks like Clip Text does nothing. When changing font size , the button fits the size of the font, but I need the button to stay specific size.
How to keep a button size independent of it’s content?
Clip_text clips text only horizontally, which won’t add new line, but not vertical(text height).
Clip_content clips children(you can try to clip_content on parent node).
Use a RichTextLabel as a button. They will not resize for your text and will cut it off like you seem to want. Then link the meta_clicked signal to make it work like a button.
Yes, this approach works. However the text in rich label doesn’t center vertically and don’t store hover theme, so I made my custom Button with a Label inside.
@tool
class_name ButtonPlus extends Button
@export_category("Content")
@export var label_text: String = "" :
set(value):
if label_text != value:
label_text = value
if label:
_refresh_label()
@export_range(0.1, 1) var text_scale: float = 0.565 : # Button pixel height vs Text pixel size
set(value):
if text_scale != value:
text_scale = value
_update_label_size()
@export_category("Debug")
@export var refresh: bool:
set(value):
_refresh_label()
@onready var label: Label = $Label
func _ready() -> void:
_refresh_label()
# Label
func _update_label_text() -> void:
if label:
label.text = label_text
func _update_label_size() -> void:
if label:
var button_height: int = roundi(size.y)
label.add_theme_font_size_override("font_size", roundi(button_height * text_scale))
func _refresh_label() -> void:
if label:
_update_label_text()
_update_label_size()
func _on_resized() -> void:
_update_label_size()
This also makes buttons to display text of the same size at different resolutions.