19 lines
767 B
TypeScript
19 lines
767 B
TypeScript
import { Children, type ReactNode } from 'react';
|
|
import { Platform, Text as RNText, type TextProps } from 'react-native';
|
|
|
|
const WORD_JOINER = '\u2060';
|
|
const HANGUL = /[\uAC00-\uD7A3]/;
|
|
|
|
/** 안드로이드는 한글을 글자마다 끊어서, 단어 안쪽에 줄바꿈 금지 문자를 넣어 띄어쓰기에서만 줄이 바뀌게 한다 */
|
|
function keepWords(s: string): string {
|
|
return HANGUL.test(s) ? s.replace(/\S+/g, (w) => Array.from(w).join(WORD_JOINER)) : s;
|
|
}
|
|
|
|
function wrap(children: ReactNode): ReactNode {
|
|
return Children.map(children, (c) => (typeof c === 'string' ? keepWords(c) : c));
|
|
}
|
|
|
|
export function Text({ children, ...props }: TextProps) {
|
|
return <RNText {...props}>{Platform.OS === 'android' ? wrap(children) : children}</RNText>;
|
|
}
|