๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

React Native

[ React Native ] React Native ์—์„œ Text ๊ธธ์ด ์กฐ์ ˆ ๋ฐ ์ค„๋ฐ”๊ฟˆ ํ•˜๊ธฐ.

์ถœ์ฒ˜ - React Native ๊ณต์‹ ํŽ˜์ด์ง€ Text ๋ฌธ์„œ

 

 

 

React Native์—์„œ ํ…์ŠคํŠธ์˜ ์ค„ ๊ธธ์ด๋ฅผ flexible ํ•˜๊ฒŒ ๊ธธ์ด ์กฐ์ ˆ ๋ฐ ์ค„ ๋ฐ”๊ฟˆ์„ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” <Text> ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ ์ค‘numberOfLines ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

<View style={{width: '100%', padding:20}}>
  <Text numberOfLines={1} ellipsizeMode="tail" style={{fontSize:18, fontWeight: 'bold', marginBottom:10}}>
    ํ•œ๊ธ€์— ๋Œ€ํ•˜์—ฌ
  </Text>
  <Text numberOfLines={5} ellipsizeMode="tail" style={{fontSize:14}}>
    ํ›ˆ๋ฏผ์ •์Œ ํ•ด๋ก€๋ณธ]์€ ์„ธ์ข…์ด ์ง์ ‘ ์„œ๋ฌธ์„ ์“ฐ๊ณ  ์ •์ธ์ง€ ๊ฐ™์€ ์‹ ํ•˜๋“ค์—๊ฒŒ ๊ธ€์ž์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ ๊ฒŒ ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 
    ์ด ์ฑ…์ด 1940๋…„์— ์•ˆ๋™์—์„œ ๋ฐœ๊ฒฌ๋  ๋•Œ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š” ํ•œ๊ธ€์˜ ์ฐฝ์ œ ์›๋ฆฌ์— ๋Œ€ํ•ด ์ „ํ˜€ ๋ชจ๋ฅด๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 
    ๊ทธ๋Ÿฌ๋‹ค ์ด ์ฑ…์ด ๋ฐœ๊ฒฌ๋จ์œผ๋กœ ํ•ด์„œ ํ•œ๊ธ€์ด ์–ผ๋งˆ๋‚˜ ๊ณผํ•™์ ์ธ ์›๋ฆฌ๋กœ ๋งŒ๋“ค์–ด์กŒ๋Š”์ง€ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ต๋‹ˆ๋‹ค. 
    ์ด ์ฑ…์ด ์šฐ๋ฆฌ์—๊ฒŒ ์ „ํ•ด์ง„ ๊ฒƒ์€ ์ „์ ์œผ๋กœ ์„ฑ๋ถ๋™์— ์žˆ๋Š” ๊ฐ„์†ก๋ฏธ์ˆ ๊ด€์„ ์„ธ์šด ์ „ํ˜•ํ•„ ์„ ์ƒ์˜ ๊ณต์ž…๋‹ˆ๋‹ค. 
    ์„ ์ƒ์€ ์•„์ฃผ ๋น„์‹ผ ๊ฐ€๊ฒฉ์œผ๋กœ ์ด ์ฑ…์„ ์ƒ€๊ณ  6โˆ™25 ๋•Œ์—๋„ ์ด ์ฑ… ํ•œ ๊ถŒ๋งŒ ๋“ค๊ณ  ํ”ผ๋‚œ ๊ฐˆ ์ •๋„๋กœ ์ด ์ฑ…์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด ๋ชธ์„ ๋ฐ”์นœ ๋ถ„์ž…๋‹ˆ๋‹ค. 
    ์ด ๋ถ„๋„ [์ง์ง€]๋ฅผ ์„ธ๊ณ„์— ์•Œ๋ฆฐ ๋ฐ•๋ณ‘์„  ์„ ์ƒ์ฒ˜๋Ÿผ ์šฐ๋ฆฌ์˜ ๋ฌธํ™”์˜์›…์ž…๋‹ˆ๋‹ค.
    [๋„ค์ด๋ฒ„ ์ง€์‹๋ฐฑ๊ณผ] ํ•œ๊ธ€ - ์„ธ์ƒ์—์„œ ๊ฐ€์žฅ ์‹ ๋น„ํ•œ ๋ฌธ์ž (์œ„๋Œ€ํ•œ ๋ฌธํ™”์œ ์‚ฐ, ์ตœ์ค€์‹)
   </Text>
</View>

 

์œ„์™€ ๊ฐ™์ด <Text numberOfLines={๋ผ์ธ์˜ ์ˆ˜}> ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๋ฐ, ๋ณดํ†ต ellipsizeMode(์ƒ๋žต์— ๋Œ€ํ•œ ๋ชจ๋“œ) ์†์„ฑ๋„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค. 

 

 

 

 

ellipsizeMode ๋Š” ์„ค์ •์„ ์•ˆ ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ, default ๊ฐ’์€ 'tail' ์ด๋ฉฐ,  'head' | 'middle' | 'tail' | 'clip'  ๋กœ ์ด 4๊ฐ€์ง€ ๋ชจ๋“œ๊ฐ€ ์žˆ๋‹ค.

 

 

 

 

 

1. head ๋ชจ๋“œ

   ellipsizeMode='head'  

 

ํ…์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์ค„ head ๋ถ€๋ถ„์— ใƒปใƒปใƒป ํ‘œ์‹œ๊ฐ€ ๋‚˜์˜จ๋‹ค. (์ฒ˜์Œ 4์ค„ + ๋งˆ์ง€๋ง‰ ์ค„์ด ๋‚˜์˜ด.)

์ฒ˜์Œ๋ถ€ํ„ฐ 4๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๋‚˜์˜ค๊ณ , ๋งˆ์ง€๋ง‰ ์ค„์˜ head ๋ถ€๋ถ„์—  ใƒปใƒปใƒป  ํ‘œ์‹œ๊ฐ€ ๋‚˜์˜ค๋ฉฐ, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ํ…์ŠคํŠธ๊ฐ€ ๋’ค์— ์ด์–ด์„œ ๋‚˜์˜จ๋‹ค.

 

 

 

 

 

2 .middle ๋ชจ๋“œ 

   ellipsizeMode='middle'  

 

ํ…์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์ค„ ์ค‘๊ฐ„ ๋ถ€๋ถ„์— ใƒปใƒปใƒป ํ‘œ์‹œ๊ฐ€ ๋‚˜์˜จ๋‹ค. (์ฒ˜์Œ 4์ค„ + ๋งˆ์ง€๋ง‰ ์ค„์ด ๋‚˜์˜ด.)

์ฒ˜์Œ๋ถ€ํ„ฐ 4๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๋‚˜์˜ค๊ณ , ๋งˆ์ง€๋ง‰ ์ค„์˜ ์ค‘๊ฐ„ ๋ถ€๋ถ„์—  ใƒปใƒปใƒป  ํ‘œ์‹œ๊ฐ€ ๋‚˜์˜ค๋ฉฐ, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ํ…์ŠคํŠธ๊ฐ€ ๋’ค์— ์ด์–ด์„œ ๋‚˜์˜จ๋‹ค.

 

 

 

 

 

3 .tail ๋ชจ๋“œ

   ellipsizeMode='tail'  

 

ํ…์ŠคํŠธ์˜ ๋งจ ๋งˆ์ง€๋ง‰์— ใƒปใƒปใƒป ํ‘œ์‹œ๊ฐ€ ๋‚˜์˜จ๋‹ค.

ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ 5์ค„ ๋‚˜์˜ค๊ณ  ๋งˆ์ง€๋ง‰ ์ค„์˜ ๋ ๋ถ€๋ถ„์—  ใƒปใƒปใƒป  ํ‘œ์‹œ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

 

 

 

 

 

4 .clip ๋ชจ๋“œ

   ellipsizeMode='clip'  

 

ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ 5์ค„ ๋‚˜์˜ค๊ณ  ๋งˆ์ง€๋ง‰์— ํ…์ŠคํŠธ๊ฐ€ ์ž˜๋ ค์„œ ๋‚˜์˜จ๋‹ค.

 

 

 

 

 

 

 

 

 

React Native Text ์ปดํฌ๋„ŒํŠธ์˜ ellipsizeMode ์™€ numberOfLines ์„ค๋ช….


/**
     * This can be one of the following values:
     *
     * - `head` - The line is displayed so that the end fits in the container and the missing text
     * at the beginning of the line is indicated by an ellipsis glyph. e.g., "...wxyz"
     * - `middle` - The line is displayed so that the beginning and end fit in the container and the
     * missing text in the middle is indicated by an ellipsis glyph. "ab...yz"
     * - `tail` - The line is displayed so that the beginning fits in the container and the
     * missing text at the end of the line is indicated by an ellipsis glyph. e.g., "abcd..."
     * - `clip` - Lines are not drawn past the edge of the text container.
     *
     * The default is `tail`.
     *
     * `numberOfLines` must be set in conjunction with this prop.
     *
     * > `clip` is working only for iOS
     */
    ellipsizeMode?: 'head' | 'middle' | 'tail' | 'clip' | undefined;
    
    
      /**
     * Used to truncate the text with an ellipsis after computing the text
     * layout, including line wrapping, such that the total number of lines
     * does not exceed this number.
     *
     * This prop is commonly used with `ellipsizeMode`.
     */
    numberOfLines?: number | undefined;