vanilladocs
vanillapunch.com
API / core/component/richtext

core/component/richtext

마크업 색상 이름 → HEX 매핑. (Unity TMP 와 호환)

Markup color name → HEX mapping. (compatible with Unity TMP)

RichTextextends Text

짝Counterpart: vanilla-engine RichText

리치텍스트 출력기 컴포넌트. (Text 를 상속)

  • 마크업 (Unity TMP 호환) 을 자동으로 파싱해 segment 별 폰트 / 크기 / 색상 / 볼드 / 이탤릭 / 밑줄 / 취소선을 다르게 적용해 그린다.
  • 지원 태그:
  • <b>...</b> : 볼드
  • <i>...</i> : 이탤릭
  • <u>...</u> : 밑줄
  • <s>...</s> : 취소선
  • <color=#RRGGBB[AA]>...</color> : 색상 (HEX)
  • <color=red>...</color> : 색상 (이름)
  • <size=N>...</size> : 폰트 크기
  • 알 수 없는 태그는 그대로 텍스트로 출력.
  • 마크업이 없는 plain text 면 부모 (Text) 의 draw 동작으로 fallback.
  • 글자 방향: segment 를 이은 글로 문단 방향을 정하고 (이 글자의 방향, "inherit" 면 Graphic 의 방향), 오른쪽에서 왼쪽이면 segment 를 오른쪽부터 놓는다. segment 마다 그 방향으로 굽고, 정렬 "start" / "end" 도 그 방향으로 푼다.
  • segment 별 미지정 속성은 부모 Text 의 전역 속성을 fallback.
  • 비활성화 상태 (isEnable() === false) 면 draw / 측정에서 건너뛴다.

Rich text renderer component. (inherits Text)

  • Automatically parses markup (Unity TMP compatible) and draws with font / size / color / bold / italic / underline / strikethrough applied differently per segment.
  • Supported tags:
  • <b>...</b> : bold
  • <i>...</i> : italic
  • <u>...</u> : underline
  • <s>...</s> : strikethrough
  • <color=#RRGGBB[AA]>...</color> : color (HEX)
  • <color=red>...</color> : color (name)
  • <size=N>...</size> : font size
  • Unknown tags are output as text as is.
  • Plain text without markup falls back to the parent (Text) draw behavior.
  • Text direction: determines the paragraph direction from the joined segment text (this text's direction, or Graphic's direction if "inherit"), and places segments from the right if right to left. Each segment is baked in that direction, and alignment "start" / "end" is also resolved by that direction.
  • Unspecified per-segment properties fall back to the parent Text's global properties.
  • When disabled (isEnable() === false), skipped in draw / measurement.
예제Example마크업으로 글 조각마다 꾸미기Style each piece of text with markup
import { Engine, EngineConfiguration, Scene, Vector2, WorldNode, RichText } from "./build/vanilla.js";

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		const labelNode = new WorldNode();
		labelNode.setContentSize(Vector2.create(560, 60));
		labelNode.setLocalPosition(Vector2.create(320, 180));
		const richText = labelNode.addComponent(RichText);
		richText.setFontSize(28);
		richText.setTextColor("#ffffff");
		richText.setText("Get <color=#ffcc00><b>100</b></color> gold and <i>a <u>key</u></i>");
		const root = this.getRoot();
		root.addChild(labelNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
생성자constructorconstructor()#0.2.1 부터since 0.2.1소스source

생성.

Constructor.

짝Counterpart: vanilla-engine RichText.new

메서드methodcalculateDrawnRect(rect)#0.4.0 부터since 0.4.0소스source

그리는 사각형을 rect 에 적기. (오버라이드: 조각이 없으면 Text 와 같고, 있으면 조각을 이은 한 줄의 사각형)

  • 조각마다 제 글꼴로 잰 너비를 더한 합산 폭을 정렬대로 놓고(그리기와 같은 자리), 가장 큰 글꼴 크기의 반과 가장 굵은 외곽선만큼 양옆에 더한다. "start" 와 "end" 는 그릴 때의 방향으로 왼쪽이나 오른쪽이 되므로 둘 다 품는다.
  • 세로는 베이스라인 자리에서 위아래로 가장 큰 글꼴 크기의 1.5 배와 가장 굵은 외곽선만큼. (밑줄, 취소선도 이 안)
  • 잴 수 없으면 false. (거르지 않는다)

Writes the drawn rectangle to rect. (Override: same as Text without segments; with segments, the rectangle of the joined single line)

  • The summed width of each segment measured in its own font is placed by alignment (same as drawing) and grown by half the largest font size plus the thickest stroke on both sides. "start" and "end" become left or right depending on the direction at draw time, so both are covered.
  • Vertically, from the baseline position up and down by 1.5 times the largest font size plus the thickest stroke. (Underline and strikethrough fit inside)
  • false if it cannot be measured. (not culled)
rectRect
그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)Where to write the drawn rectangle. (origin at the top-left of the node's content area)

반환Returnsboolean 적었으면 참. (잴 수 없으면 거짓)True if written. (False if it cannot be measured)

예제Example조각을 이은 글이 그리는 곳 보기Seeing where segmented text draws
import { WorldNode, RichText, Rect, Vector2 } from "./build/vanilla.js";

const node = new WorldNode();
node.setContentSize(Vector2.create(200, 40));
const richText = node.addComponent(RichText);
richText.appendSegment("HP ", { bold: true });
richText.appendSegment("42", { fontSize: 32 });
const drawnRect = Rect.zero();
const isKnown = richText.calculateDrawnRect(drawnRect);
console.log(isKnown, drawnRect.size.y >= 32);
메서드methodsetText(text)#0.2.1 부터since 0.2.1소스source

텍스트 설정, 마크업 자동 파싱.

  • 알려진 태그가 있으면 segment 배열로 분해해 보관.
  • 마크업이 없으면 segment 비우고 plain text 동작으로 fallback.

Sets the text, automatically parsing markup.

  • If there are known tags, splits it into a segment array and stores it.
  • If there is no markup, clears the segments and falls back to plain text behavior.
textstring
보일 글. (알려진 마크업 태그가 있으면 segment 로 나눈다)Text to show. (split into segments if it has known markup tags)

짝Counterpart: vanilla-engine RichText.set_text

메서드methodappendSegment(text, attributes)#0.2.1 부터since 0.2.1소스source

segment 1 개 추가. (편의 API, 마크업 외에 프로그래밍 방식으로 segment 쌓기)

Adds 1 segment. (convenience API, for stacking segments programmatically besides markup)

textstring
segment 글.Segment text.
attributesobject
segment 속성. ({ fontFace, fontSize, textColor, strokeColor, strokeWidth, bold, italic, underline, strikethrough }, 색은 Color, 빠진 것은 Text 의 속성을 쓴다)Segment attributes. ({ fontFace, fontSize, textColor, strokeColor, strokeWidth, bold, italic, underline, strikethrough }, colors are Color, missing ones use the Text attributes)

반환ReturnsRichText 이 컴포넌트. (이어 부를 수 있다)This component. (calls can be chained)

짝Counterpart: vanilla-engine RichText.append_segment

예제Examplesegment 를 코드로 쌓기Build segments in code
import { Engine, EngineConfiguration, Scene, Vector2, WorldNode, RichText, Color } from "./build/vanilla.js";

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		const labelNode = new WorldNode();
		labelNode.setContentSize(Vector2.create(400, 60));
		labelNode.setLocalPosition(Vector2.create(320, 180));
		const richText = labelNode.addComponent(RichText);
		richText.setFontSize(24);
		const healthColor = Color.createFromHEX("#ff4444");
		richText.appendSegment("HP ", { bold: true });
		richText.appendSegment("42", { textColor: healthColor, fontSize: 32 });
		richText.appendSegment(" / 100");
		const segments = richText.getSegments();
		console.log(segments.length);
		const root = this.getRoot();
		root.addChild(labelNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
메서드methodclearSegments()#0.2.1 부터since 0.2.1소스source

segment 모두 제거.

Removes all segments.

짝Counterpart: vanilla-engine RichText.clear_segments

메서드methodgetSegments()#0.2.1 부터since 0.2.1소스source

segment 목록 반환. (사본)

Returns the segment list. (copy)

반환Returns{ text: string, attributes: object }[] segment 목록의 사본. ({ text, attributes } 배열)Copy of the segment list. (an array of { text, attributes })

짝Counterpart: vanilla-engine RichText.get_segments

메서드methodhasSegments()#0.2.1 부터since 0.2.1소스source

segment 보유 여부.

Whether it has segments.

반환Returnsboolean 글이 있는 segment 가 하나라도 있으면 참.True if at least one segment has text.

짝Counterpart: vanilla-engine RichText.has_segments

메서드methoddraw(graphic)#0.2.1 부터since 0.2.1소스source

출력. (비활성화면 건너뜀, segment 가 비어있으면 부모의 plain text 동작)

Draw. (skipped if disabled; the parent's plain text behavior if segments are empty)

graphicGraphic
그래픽.Graphics.
메서드methoddrawSegments(graphic, contentSize)#0.2.1 부터since 0.2.1소스source

segment 별로 폰트 / 색 / 스타일을 적용해 좌→우 누적 그리기.

  • 정렬 / baseline 은 합산 폭 / 최대 폰트 크기 기준.

Draws cumulatively left→right applying font / color / style per segment.

  • Alignment / baseline are based on the total width / maximum font size.
graphicGraphic
그래픽.Graphics.
contentSizeVector2
노드의 내용 크기.The node's content size.

짝Counterpart: vanilla-engine RichText.draw_segments

메서드methodresolveSegmentFontFace(input, fallback)#0.2.1 부터since 0.2.1소스source

segment 의 fontFace 입력값을 해석해 적용 가능한 FontFace 로 변환.

Interprets a segment's fontFace input and converts it to an applicable FontFace.

inputFontFace | FontAsset | null | undefined
segment 의 글꼴 값. (FontAsset 이면 그 글꼴, null 이나 undefined 면 fallback)Segment font value. (its font if a FontAsset, fallback if null or undefined)
fallbackFontFace | null
대신 쓸 글꼴.Font to use instead.

반환ReturnsFontFace | null 쓸 글꼴. (null 이면 시스템 글꼴)Font to use. (system font if null)

메서드methodgetIntrinsicContentSize()#0.2.1 부터since 0.2.1소스source

텍스트의 자연 크기 반환.

  • segment 가 있으면 segment 합산 폭 + 최대 폰트 크기.
  • 없으면 부모 (Text) 의 plain text 측정.

Returns the natural size of the text.

  • If there are segments, the total segment width + maximum font size.
  • Otherwise, the parent (Text) plain text measurement.

반환ReturnsVector2 자연 크기. (segment 너비 합과 가장 큰 글자 크기, 잴 수 없거나 비활성이면 (-1, -1))Intrinsic size. (sum of the segment widths and the largest font size, (-1, -1) if it cannot be measured or is disabled)