# ui/uilabel (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uilabel/

UI label. (UILabel of the original ui/uilabel.js)
- A display-only widget. Draws text aligned within the host's content_size area. Setters pass through to the text component as in the original.
- The original attaches Text and RichText to the host with require() and holds their references. The Rust version embeds one RichText, and RichText embeds Text.
  (the same form as Sprite embedding Paint) This is so a label.set_text call from scene code outside tick takes effect immediately.
  A sibling Text would be reachable only inside tick and lag one frame. So `get_component::<Text>()` on a label node is None.
  The embedded Text is taken out with get_text_component / get_text_component_mut, and RichText with get_rich_text_component / get_rich_text_component_mut.
- The original applies setters to both Text and RichText and picks which one to turn on with use_rich_text. The Rust version has a single set, so there is nothing to keep in sync.
  With use_rich_text(true) it draws with RichText (parsing markup), otherwise with the embedded Text. append_segment / clear_segments pass through to RichText.
- The original setFont(FontFace) was changed to set_font_family (a registered font name).
- Text direction (set_text_direction) and the visible character count (set_visible_character_count) pass through to the text component as in the original.
- Not ported from the original: invalidateIntrinsicContentSize (UINode path only).
- The background color (UIView) is drawn before the text. (in the original too, UILabel is attached ahead of the Text and RichText it requires, so the background is drawn first)

## UILabel (struct) (Default, Component)

UI label.
- Embeds RichText (RichText embeds Text) and passes the setters through. Draws aligned within the host's content_size area, painting the background color first.

Example: A score label attached to a node

```rust
use vanilla::{NodeTree, Text, TextAlign, UILabel, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let label_id = node_tree.create_child(root_id, "score");
node_tree[label_id].set_content_size(Vector2::create(240.0, 48.0));
let label = node_tree.add_component(label_id, UILabel::create_with_text("Score 0", 28.0)).unwrap();
label.set_text_color("#ffd166");
label.set_text_align(TextAlign::Left);
label.set_text("Score 120");
assert_eq!(label.get_text(), "Score 120");
assert_eq!(label.get_text_component().get_font_size(), 28.0);
assert!(node_tree[label_id].get_component::<Text>().is_none());
```

Example: Draw each piece differently with markup

```rust
use vanilla::UILabel;

let mut label = UILabel::create_with_text("<b>Bold</b> and <color=red>red</color>", 24.0);
label.use_rich_text(true);
assert!(label.is_using_rich_text());
let segments = label.get_rich_text_component().get_segments();
assert_eq!(segments.len(), 3);
assert!(segments[0].attributes.is_bold);
assert_eq!(segments[2].text, "red");
```

- `pub fn new() -> UILabel` (constructor, since 0.3.2): Constructor. (text defaults are the same as Text: 32px, white, center aligned, middle baseline)
  - returns UILabel: New label with empty text. (rich text mode off, the content node is created when attached to a tree)
- `pub fn create_with_text(text: &str, font_size: f64) -> UILabel` (static, since 0.3.2): Constructor with text and size.
  - `text` &str: Text to show. (markup is parsed ahead, but drawn as segments only when use_rich_text(true))
  - `font_size` f64: Font size. (pixels)
  - returns UILabel: New label with that text and size.
- `pub fn get_uiview(&self) -> &UIView` (method, since 0.3.2): Returns the embedded view.
  - returns &UIView: Reference to the embedded UIView.
- `pub fn get_uiview_mut(&mut self) -> &mut UIView` (method, since 0.3.2): Returns the embedded view. (mutable)
  - returns &mut UIView: Mutable reference to the embedded UIView.
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle.
  - returns Option<NodeId>: content node. None before being attached to a tree.
- `pub fn set_background_color(&mut self, background_color: impl Into<Color>)` (method, since 0.3.2): Sets the background color.
  - `background_color` impl Into<Color>: Background color. (drawn before the text, not painted if alpha is 0)
- `pub fn get_background_color(&self) -> Color` (method, since 0.3.2): Returns the background color.
  - returns Color: Background color. (default is white with alpha 0)
- `pub fn get_text_component(&self) -> &Text` (method, since 0.3.2): Returns the embedded text component. (in place of the original node.get_component(Text))
  - returns &Text: Reference to the embedded Text. (the one inside the embedded RichText)
- `pub fn get_text_component_mut(&mut self) -> &mut Text` (method, since 0.3.2): Returns the contained text component. (mutable)
  - returns &mut Text: Mutable reference to the embedded Text.
- `pub fn get_rich_text_component(&self) -> &RichText` (method, since 0.3.2): Returns the embedded rich text component. (in place of the original node.get_component(RichText))
  - returns &RichText: Reference to the embedded RichText.
- `pub fn get_rich_text_component_mut(&mut self) -> &mut RichText` (method, since 0.3.2): Returns the embedded rich text component. (mutable)
  - returns &mut RichText: Mutable reference to the embedded RichText.
- `pub fn append_segment(&mut self, text: &str, attributes: RichTextAttributes) -> &mut UILabel` (method, since 0.3.2): Adds one rich text segment. (shown when use_rich_text(true))
  - `text` &str: Segment text. (markup is not parsed)
  - `attributes` RichTextAttributes: Segment styling. (fields not set follow the label's values)
  - returns &mut UILabel: This label, for chaining.

  Example: Stack segments in code one after another

  ```rust
  use vanilla::{RichTextAttributes, UILabel};
  
  let mut label = UILabel::new();
  label.use_rich_text(true);
  let mut bold_attributes = RichTextAttributes::new();
  bold_attributes.is_bold = true;
  label.append_segment("Level ", RichTextAttributes::new()).append_segment("3", bold_attributes);
  assert_eq!(label.get_rich_text_component().get_segments().len(), 2);
  label.clear_segments();
  assert!(label.get_rich_text_component().get_segments().is_empty());
  ```

- `pub fn clear_segments(&mut self)` (method, since 0.3.2): Clears all rich text segments.
- `pub fn use_rich_text(&mut self, use_rich_text: bool)` (method, since 0.3.2): Sets the rich text mode. (if true, parses markup and draws each segment differently)
  - `use_rich_text` bool: If true, draws with the embedded RichText (per segment), if false, with the embedded Text.
- `pub fn is_using_rich_text(&self) -> bool` (method, since 0.3.2): Whether rich text mode is on.
  - returns bool: true if in rich text mode.
- `pub fn set_text(&mut self, text: &str)` (method, since 0.3.2): Sets the text.
  - `text` &str: New text. (skipped if not in rich text mode and the same as the current text, markup is always parsed into segments)
- `pub fn get_text(&self) -> &str` (method, since 0.3.2): Returns the text.
  - returns &str: Current text. (the original text with markup)
- `pub fn set_fit_width(&mut self, width: f64, minimum_ratio: f64)` (method, since 0.4.0): Sets fitting single-line text to a width. (set_fit_width of the embedded Text, rich text mode does not fit)
  - `width` f64: Width to fit. (pixels, no fitting if 0 or less)
  - `minimum_ratio` f64: Minimum ratio the font size can shrink to. (clamped to 0 ~ 1)

  Example: Fit to a 180 pixel button slot but do not shrink below 60%

  ```rust
  use vanilla::UILabel;
  
  let mut label = UILabel::create_with_text("Start", 32.0);
  label.set_fit_width(180.0, 0.6);
  assert_eq!(label.get_fit_width(), 180.0);
  assert_eq!(label.get_font_size(), 32.0);
  label.set_fit_width(-5.0, 0.6);
  assert_eq!(label.get_fit_width(), 0.0);
  ```

- `pub fn get_fit_width(&self) -> f64` (method, since 0.4.0): Returns the width to fit. (no fitting if 0)
  - returns f64: Width to fit. (pixels, no fitting if 0)
- `pub fn calculate_fitted_font_size(&mut self, graphic: &mut Graphic) -> f64` (method, since 0.4.0): Measures and returns the drawn size on the spot.
  - `graphic` &mut Graphic: Graphic to measure the text with.
  - returns f64: Font size when drawn. (pixels, the shrunk size if it overflows the fit width)
- `pub fn measure_text(&self, graphic: &Graphic, text: &str, font_size: f64) -> f64` (method, since 0.4.0): Measures the width of text. (measure_text of the embedded Text)
  - `graphic` &Graphic: Graphic to measure the text with.
  - `text` &str: Text to measure. (separate from the label's text)
  - `font_size` f64: Font size to measure with. (pixels)
  - returns f64: Width of the text. (pixels, 0 for empty text)
- `pub fn get_fitted_font_size(&self) -> f64` (method, since 0.4.0): Returns the drawn size. (the value from the last draw or measurement)
  - returns f64: Font size when drawn. (pixels, the set size if the layout has not been remeasured yet)
- `pub fn set_font_size(&mut self, font_size: f64)` (method, since 0.3.2): Sets the font size.
  - `font_size` f64: Font size. (pixels)
- `pub fn get_font_size(&self) -> f64` (method, since 0.3.2): Returns the font size.
  - returns f64: The set font size. (pixels, unchanged even when shrunk to fit)
- `pub fn set_font_family(&mut self, font_family: &str)` (method, since 0.3.2): Sets the font name. (in place of the original setFont. a name registered with Graphic, the default font if none)
  - `font_family` &str: Font name. (a name registered with Graphic)
- `pub fn get_font_family(&self) -> &str` (method, since 0.3.2): Returns the font name.
  - returns &str: Font name.
- `pub fn set_word_wrap_width(&mut self, word_wrap_width: f64)` (method, since 0.3.2): Sets the word wrap width. (0 splits only at newline characters)
  - `word_wrap_width` f64: Word wrap width. (pixels, 0 splits only at newline characters)
- `pub fn set_wrap_mode(&mut self, wrap_mode: TextWrapMode)` (method, since 0.4.0): Sets the wrap mode. (original setWrapMode, Word or Character)
  - `wrap_mode` TextWrapMode: Wrap mode. (Word wraps by words, Character by characters)
- `pub fn get_wrap_mode(&self) -> TextWrapMode` (method, since 0.4.0): Returns the wrap mode.
  - returns TextWrapMode: Wrap mode.
- `pub fn set_line_spacing(&mut self, line_spacing: f64)` (method, since 0.3.2): Sets the line spacing multiplier.
  - `line_spacing` f64: Line spacing. (multiple of the font size)
- `pub fn set_letter_spacing(&mut self, letter_spacing: f64)` (method, since 0.4.0): Sets the letter spacing. (same meaning as CSS letter-spacing, original setLetterSpacing)
  - `letter_spacing` f64: Spacing added after each grapheme cluster. (pixels, negative narrows, default 0)
- `pub fn get_letter_spacing(&self) -> f64` (method, since 0.4.0): Returns the letter spacing.
  - returns f64: Letter spacing. (pixels, default 0)
- `pub fn set_visible_character_count(&mut self, visible_character_count: i32)` (method, since 0.4.0): Sets the visible character count. (-1 for all, for typewriter effects. original setVisibleCharacterCount)
  - `visible_character_count` i32: Visible character count. (-1 for all, below -1 is set to -1. text with markup is not cut)
- `pub fn get_visible_character_count(&self) -> i32` (method, since 0.4.0): Returns the visible character count.
  - returns i32: Visible character count. (-1 for all)
- `pub fn set_stroke_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the outline color.
  - `color` impl Into<Color>: Stroke color.
- `pub fn set_stroke_width(&mut self, stroke_width: f64)` (method, since 0.3.2): Sets the outline thickness.
  - `stroke_width` f64: Outline thickness. (pixels, 0 for no outline)
- `pub fn set_text_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the text color.
  - `color` impl Into<Color>: Text color. (a Color or text like "#rrggbb")
- `pub fn get_text_color(&self) -> Color` (method, since 0.3.2): Returns the text color.
  - returns Color: Text color.
- `pub fn set_bold(&mut self, is_bold: bool)` (method, since 0.3.2): Sets bold.
  - `is_bold` bool: If true, bold.
- `pub fn is_bold(&self) -> bool` (method, since 0.3.2): Whether bold.
  - returns bool: true if bold.
- `pub fn set_italic(&mut self, is_italic: bool)` (method, since 0.3.2): Sets italic.
  - `is_italic` bool: If true, italic.
- `pub fn is_italic(&self) -> bool` (method, since 0.3.2): Whether italic.
  - returns bool: true if italic.
- `pub fn set_underline(&mut self, is_underline: bool)` (method, since 0.4.0): Sets underline.
  - `is_underline` bool: If true, underlined.
- `pub fn is_underline(&self) -> bool` (method, since 0.4.0): Whether underlined.
  - returns bool: true if underlined.
- `pub fn set_strikethrough(&mut self, is_strikethrough: bool)` (method, since 0.4.0): Sets strikethrough.
  - `is_strikethrough` bool: If true, strikethrough.
- `pub fn is_strikethrough(&self) -> bool` (method, since 0.4.0): Whether strikethrough.
  - returns bool: true if strikethrough.
- `pub fn set_text_direction(&mut self, text_direction: TextDirection)` (method, since 0.4.0): Sets the text direction. (Inherit | Ltr | Rtl | Auto, original setTextDirection. Inherit uses the Graphic direction at draw time)
  - `text_direction` TextDirection: Text direction.
- `pub fn get_text_direction(&self) -> TextDirection` (method, since 0.4.0): Returns the text direction.
  - returns TextDirection: Text direction.
- `pub fn set_text_align(&mut self, text_align: TextAlign)` (method, since 0.3.2): Sets the horizontal alignment.
  - `text_align` TextAlign: Horizontal alignment within the content area.
- `pub fn get_text_align(&self) -> TextAlign` (method, since 0.3.2): Returns the horizontal alignment.
  - returns TextAlign: Horizontal alignment.
- `pub fn set_text_baseline(&mut self, text_baseline: TextBaseline)` (method, since 0.3.2): Sets the vertical baseline.
  - `text_baseline` TextBaseline: Vertical baseline within the content area.
- `pub fn get_text_baseline(&self) -> TextBaseline` (method, since 0.3.2): Returns the vertical baseline.
  - returns TextBaseline: Vertical baseline.
