UILabel: text property
The text property is the text the label shows. The whole text uses the single font and color given by font and textColor.
Value
A string, or null.
- Readable and writable.
- The default is
null, meaning no text. - After writing, the text on screen updates immediately. When positioning with constraints, the label's size is recalculated for the new text; you do not need to refresh the layout manually. The automatic size change is not verified in the simulator.
- Values that are not strings are converted with
String()on write:label.text = 5shows "5".nullandundefinedmean no text. - After
attributedTexthas been written,textreads as the plain text in the attributed string.
Note: If a label has shown an attributed string (
attributedTextwas written) and you then writetext, the text is replaced, but the styles from the original attributed string remain; for example, the underline is still there. To change the content of such a label, keep writingattributedText.
Examples
Changing the text
js
const label = new UILabel();
label.text = 'New title';Effect: The text shown by the label changes to "New title".
Clear the text
js
const label = new UILabel();
label.text = 'Temporary';
label.text = null;
console.log(label.text);Effect: The label shows no text, and the console prints null. The effect on screen is not verified in the simulator.
Corresponding iOS API
UILabel.text.
See also
attributedText: text mixing several fonts and colorsfont,textColor