# ui/uilabel (vanilla-engine 0.4.0)

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

UI 라벨. (원본 ui/uilabel.js 의 UILabel)
- 표시 전용 위젯. 호스트 content_size 영역 안에 정렬해 글자를 그린다. 세터는 원본처럼 글자 컴포넌트에 넘긴다.
- 원본은 require() 로 호스트에 Text 와 RichText 를 붙이고 그 참조를 든다. 러스트 판은 RichText 하나를 품고, RichText 가 Text 를 품는다.
  (Sprite 가 Paint 를 품는 것과 같은 꼴) 씬 코드가 tick 밖에서 label.set_text 를 불러도 바로 반영되게 하려는 것이다.
  형제 Text 를 두면 tick 안에서만 닿아 한 프레임 늦는다. 그래서 라벨 노드의 `get_component::<Text>()` 는 None 이다.
  품은 Text 는 get_text_component / get_text_component_mut, RichText 는 get_rich_text_component / get_rich_text_component_mut 로 꺼낸다.
- 원본은 Text 와 RichText 두 벌에 세터를 같이 걸고 use_rich_text 로 어느 쪽을 켤지 고른다. 러스트 판은 한 벌이라 맞출 일이 없다.
  use_rich_text(true) 면 RichText 로(마크업 해석), 아니면 품은 Text 로 그린다. append_segment / clear_segments 는 RichText 에 넘긴다.
- 원본 setFont(FontFace) 는 set_font_family(등록한 폰트 이름) 로 바꿨다.
- 글자 방향(set_text_direction)과 표시 글자 수(set_visible_character_count)는 원본처럼 글자 컴포넌트에 넘긴다.
- 원본에서 옮기지 않은 것: invalidateIntrinsicContentSize (UINode 노선 전용).
- 배경색(UIView)은 글자보다 먼저 그린다. (원본도 UILabel 이 require 한 Text, RichText 보다 앞 차례로 붙어 배경을 먼저 그린다)

## UILabel (struct) (Default, Component)

UI 라벨.
- RichText 를 품고(RichText 가 Text 를 품는다) 세터를 넘긴다. 호스트 content_size 영역 안에 정렬해 그리고, 배경색을 먼저 칠한다.

예제: 노드에 붙인 점수 라벨

```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());
```

예제: 마크업으로 조각마다 다르게 그리기

```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): 생성. (글자 기본값은 Text 와 같다: 32px, 흰색, 가운데 정렬, 가운데 기준선)
  - returns UILabel: 글이 빈 새 라벨. (리치텍스트 모드 꺼짐, content 노드는 트리에 붙일 때 만든다)
- `pub fn create_with_text(text: &str, font_size: f64) -> UILabel` (static, since 0.3.2): 글과 크기로 생성.
  - `text` &str: 보일 글. (마크업을 풀어 두지만 use_rich_text(true) 일 때만 조각으로 그린다)
  - `font_size` f64: 글자 크기. (픽셀)
  - returns UILabel: 그 글과 크기의 새 라벨.
- `pub fn get_uiview(&self) -> &UIView` (method, since 0.3.2): 품은 뷰 반환.
  - returns &UIView: 품은 UIView 의 참조.
- `pub fn get_uiview_mut(&mut self) -> &mut UIView` (method, since 0.3.2): 품은 뷰 반환. (변경용)
  - returns &mut UIView: 품은 UIView 의 바꿀 수 있는 참조.
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): 컨텐트 노드 핸들.
  - returns Option<NodeId>: content 노드. 트리에 붙기 전이면 None.
- `pub fn set_background_color(&mut self, background_color: impl Into<Color>)` (method, since 0.3.2): 배경색 설정.
  - `background_color` impl Into<Color>: 배경색. (글자보다 먼저 그린다, 알파가 0 이면 칠하지 않는다)
- `pub fn get_background_color(&self) -> Color` (method, since 0.3.2): 배경색 반환.
  - returns Color: 배경색. (기본은 알파 0 인 흰색)
- `pub fn get_text_component(&self) -> &Text` (method, since 0.3.2): 품은 글자 컴포넌트 반환. (원본의 node.get_component(Text) 자리)
  - returns &Text: 품은 Text 의 참조. (품은 RichText 안의 것)
- `pub fn get_text_component_mut(&mut self) -> &mut Text` (method, since 0.3.2): 품은 글자 컴포넌트 반환. (변경용)
  - returns &mut Text: 품은 Text 의 바꿀 수 있는 참조.
- `pub fn get_rich_text_component(&self) -> &RichText` (method, since 0.3.2): 품은 리치텍스트 컴포넌트 반환. (원본의 node.get_component(RichText) 자리)
  - returns &RichText: 품은 RichText 의 참조.
- `pub fn get_rich_text_component_mut(&mut self) -> &mut RichText` (method, since 0.3.2): 품은 리치텍스트 컴포넌트 반환. (변경용)
  - returns &mut RichText: 품은 RichText 의 바꿀 수 있는 참조.
- `pub fn append_segment(&mut self, text: &str, attributes: RichTextAttributes) -> &mut UILabel` (method, since 0.3.2): 리치텍스트 조각 하나 더하기. (use_rich_text(true) 일 때 보인다)
  - `text` &str: 조각 글. (마크업을 풀지 않는다)
  - `attributes` RichTextAttributes: 조각의 꾸밈. (정하지 않은 칸은 라벨의 값을 따른다)
  - returns &mut UILabel: 이어 부를 수 있게 이 라벨.

  예제: 코드로 조각을 이어 쌓기

  ```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): 리치텍스트 조각 모두 지우기.
- `pub fn use_rich_text(&mut self, use_rich_text: bool)` (method, since 0.3.2): 리치텍스트 모드 설정. (true 면 마크업을 풀어 조각마다 다르게 그린다)
  - `use_rich_text` bool: true 면 품은 RichText 로(조각마다), false 면 품은 Text 로 그린다.
- `pub fn is_using_rich_text(&self) -> bool` (method, since 0.3.2): 리치텍스트 모드 여부.
  - returns bool: 리치텍스트 모드면 true.
- `pub fn set_text(&mut self, text: &str)` (method, since 0.3.2): 글 설정.
  - `text` &str: 새 글. (리치텍스트 모드가 아니고 지금 글과 같으면 건너뛴다, 마크업은 늘 풀어 조각으로 둔다)
- `pub fn get_text(&self) -> &str` (method, since 0.3.2): 글 반환.
  - returns &str: 지금 글. (마크업이 든 원래 글)
- `pub fn set_fit_width(&mut self, width: f64, minimum_ratio: f64)` (method, since 0.4.0): 한 줄 글을 너비에 맞추기 설정. (품은 Text 의 set_fit_width, 리치텍스트 모드는 맞추지 않는다)
  - `width` f64: 맞출 너비. (픽셀, 0 이하면 맞추지 않는다)
  - `minimum_ratio` f64: 글자 크기를 줄일 수 있는 가장 작은 비율. (0 ~ 1 로 자른다)

  예제: 단추 칸 180 픽셀에 맞추되 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): 맞출 너비 반환. (0 이면 맞추지 않는다)
  - returns f64: 맞출 너비. (픽셀, 0 이면 맞추지 않는다)
- `pub fn calculate_fitted_font_size(&mut self, graphic: &mut Graphic) -> f64` (method, since 0.4.0): 그릴 때의 크기를 그 자리에서 재어 반환.
  - `graphic` &mut Graphic: 글을 잴 그래픽.
  - returns f64: 그릴 때의 글자 크기. (픽셀, 맞출 너비를 넘치면 줄인 크기)
- `pub fn measure_text(&self, graphic: &Graphic, text: &str, font_size: f64) -> f64` (method, since 0.4.0): 글의 너비 재기. (품은 Text 의 measure_text)
  - `graphic` &Graphic: 글을 잴 그래픽.
  - `text` &str: 잴 글. (라벨의 글과 따로)
  - `font_size` f64: 잴 글자 크기. (픽셀)
  - returns f64: 글의 너비. (픽셀, 빈 글이면 0)
- `pub fn get_fitted_font_size(&self) -> f64` (method, since 0.4.0): 그릴 때의 크기 반환. (마지막으로 그리거나 잰 때의 값)
  - returns f64: 그릴 때의 글자 크기. (픽셀, 아직 배치를 다시 재지 않았으면 정한 크기)
- `pub fn set_font_size(&mut self, font_size: f64)` (method, since 0.3.2): 글자 크기 설정.
  - `font_size` f64: 글자 크기. (픽셀)
- `pub fn get_font_size(&self) -> f64` (method, since 0.3.2): 글자 크기 반환.
  - returns f64: 정한 글자 크기. (픽셀, 맞춰 줄여도 그대로)
- `pub fn set_font_family(&mut self, font_family: &str)` (method, since 0.3.2): 폰트 이름 설정. (원본 setFont 자리. Graphic 에 등록한 이름, 없으면 기본 폰트)
  - `font_family` &str: 폰트 이름. (Graphic 에 등록한 이름)
- `pub fn get_font_family(&self) -> &str` (method, since 0.3.2): 폰트 이름 반환.
  - returns &str: 폰트 이름.
- `pub fn set_word_wrap_width(&mut self, word_wrap_width: f64)` (method, since 0.3.2): 자동 줄바꿈 폭 설정. (0 이면 줄바꿈 문자로만 나눈다)
  - `word_wrap_width` f64: 자동 줄바꿈 폭. (픽셀, 0 이면 줄바꿈 문자로만 나눈다)
- `pub fn set_wrap_mode(&mut self, wrap_mode: TextWrapMode)` (method, since 0.4.0): 줄바꿈 방식 설정. (원본 setWrapMode, Word 또는 Character)
  - `wrap_mode` TextWrapMode: 줄바꿈 방식. (Word 는 낱말 단위, Character 는 글자 단위)
- `pub fn get_wrap_mode(&self) -> TextWrapMode` (method, since 0.4.0): 줄바꿈 방식 반환.
  - returns TextWrapMode: 줄바꿈 방식.
- `pub fn set_line_spacing(&mut self, line_spacing: f64)` (method, since 0.3.2): 줄 간격 배율 설정.
  - `line_spacing` f64: 줄 간격. (글자 크기의 배수)
- `pub fn set_letter_spacing(&mut self, letter_spacing: f64)` (method, since 0.4.0): 글자 간격 설정. (CSS letter-spacing 과 같은 뜻, 원본 setLetterSpacing)
  - `letter_spacing` f64: 글자 묶음마다 뒤에 더하는 간격. (픽셀, 음수는 좁힘, 기본 0)
- `pub fn get_letter_spacing(&self) -> f64` (method, since 0.4.0): 글자 간격 반환.
  - returns f64: 글자 간격. (픽셀, 기본 0)
- `pub fn set_visible_character_count(&mut self, visible_character_count: i32)` (method, since 0.4.0): 보일 글자 수 설정. (-1 이면 전체, 타자기 연출용. 원본 setVisibleCharacterCount)
  - `visible_character_count` i32: 보일 글자 수. (-1 이면 전체, -1 아래는 -1 로 맞춘다. 마크업이 든 글은 자르지 않는다)
- `pub fn get_visible_character_count(&self) -> i32` (method, since 0.4.0): 보일 글자 수 반환.
  - returns i32: 보일 글자 수. (-1 이면 전체)
- `pub fn set_stroke_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): 외곽선 색 설정.
  - `color` impl Into<Color>: 외곽선 색.
- `pub fn set_stroke_width(&mut self, stroke_width: f64)` (method, since 0.3.2): 외곽선 두께 설정.
  - `stroke_width` f64: 외곽선 두께. (픽셀, 0 이면 외곽선 없음)
- `pub fn set_text_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): 글자 색 설정.
  - `color` impl Into<Color>: 글자 색. (Color 나 "#rrggbb" 같은 글)
- `pub fn get_text_color(&self) -> Color` (method, since 0.3.2): 글자 색 반환.
  - returns Color: 글자 색.
- `pub fn set_bold(&mut self, is_bold: bool)` (method, since 0.3.2): 굵게 설정.
  - `is_bold` bool: true 면 굵게.
- `pub fn is_bold(&self) -> bool` (method, since 0.3.2): 굵게 여부.
  - returns bool: 굵게면 true.
- `pub fn set_italic(&mut self, is_italic: bool)` (method, since 0.3.2): 기울임 설정.
  - `is_italic` bool: true 면 기울임.
- `pub fn is_italic(&self) -> bool` (method, since 0.3.2): 기울임 여부.
  - returns bool: 기울임이면 true.
- `pub fn set_underline(&mut self, is_underline: bool)` (method, since 0.4.0): 밑줄 설정.
  - `is_underline` bool: true 면 밑줄.
- `pub fn is_underline(&self) -> bool` (method, since 0.4.0): 밑줄 여부.
  - returns bool: 밑줄이면 true.
- `pub fn set_strikethrough(&mut self, is_strikethrough: bool)` (method, since 0.4.0): 취소선 설정.
  - `is_strikethrough` bool: true 면 취소선.
- `pub fn is_strikethrough(&self) -> bool` (method, since 0.4.0): 취소선 여부.
  - returns bool: 취소선이면 true.
- `pub fn set_text_direction(&mut self, text_direction: TextDirection)` (method, since 0.4.0): 글자 방향 설정. (Inherit | Ltr | Rtl | Auto, 원본 setTextDirection. Inherit 면 그리는 때의 Graphic 방향)
  - `text_direction` TextDirection: 글자 방향.
- `pub fn get_text_direction(&self) -> TextDirection` (method, since 0.4.0): 글자 방향 반환.
  - returns TextDirection: 글자 방향.
- `pub fn set_text_align(&mut self, text_align: TextAlign)` (method, since 0.3.2): 가로 정렬 설정.
  - `text_align` TextAlign: content 영역 안의 가로 정렬.
- `pub fn get_text_align(&self) -> TextAlign` (method, since 0.3.2): 가로 정렬 반환.
  - returns TextAlign: 가로 정렬.
- `pub fn set_text_baseline(&mut self, text_baseline: TextBaseline)` (method, since 0.3.2): 세로 기준선 설정.
  - `text_baseline` TextBaseline: content 영역 안의 세로 기준선.
- `pub fn get_text_baseline(&self) -> TextBaseline` (method, since 0.3.2): 세로 기준선 반환.
  - returns TextBaseline: 세로 기준선.
