AI-generated Key Takeaways
-
A label field displays text on a block and stores the same string as both its
value
andtext
. -
Label fields can be created using JSON by specifying message strings or by explicitly interpolating labels, though the latter is discouraged for translation reasons.
-
In JavaScript, label fields are created using
Blockly.FieldLabel
and appended to block inputs usingappendField
. -
While not serializable by default, a separate "Serializable Label" field exists for cases requiring programmatic changes and serialization.
-
Label fields do not support validators due to their non-editable nature.
A label field stores a string as its value
and a string as its text
. The
value
and text
of a label field are always the same.
Label field
Label field on collapsed block
Creation
JSON
{
"type": "example_label",
"message0": "a label %1 and another label",
"args0": [
{
"type": "input_dummy"
}
]
}
Any message text between interpolation arguments becomes label strings. Alternatively, labels may be interpolated explicitly, either as an object or as text. This is generally discouraged as it makes translation more difficult.
{
"type": "example_label",
"message0": "%1 %2 %3",
"args0": [
{
"type": "field_label",
"text": "a label"
},
{
"type": "input_dummy"
},
"and another label"
]
}
JavaScript
Blockly.Blocks['example_label'] = {
init: function() {
this.appendDummyInput()
.appendField(new Blockly.FieldLabel('a label'));
this.appendDummyInput()
.appendField('and another label');
}
};
The appendField
function accepts both FieldLabel
objects and, more commonly, strings to create
labels.
The label field takes in an optional value, and an optional css class string. Both default to an empty string.
Serialization
Label fields are not serializable.
If you would like your label to be serialized, because it is being changed programmatically, see the Serializable Label field.
Validators
Label fields do not support validators, because they are not editable.