본문 바로가기

android studio

[Android] Text 사용법 (Jetpack Compose)

Text() 사용법

text() 기본 사용법

@Composable
fun SimpleText() {
    Text(text = "안녕하세요, Jetpack Compose!")
}

 

Text() 속성 정리

 - modifier (크기, 배치 등 조정)

  • Modifier를 사용하여 Text()의 크기, 정렬, 배경색 등을 조절
Text(
    text = "Hello, Compose!",
    modifier = Modifier
        .padding(16.dp) // 안쪽 여백
        .background(Color.LightGray) // 배경 색상
        .fillMaxWidth() // 가로 너비 채우기
)

 - color (글자 색상 지정)

  • 기본 색상: Color.Black, Color.White, Color.Red, Color.Blue 등
  • 커스텀 색상: Color(0xFFFF0000)
Text(text = "색상이 있는 텍스트", color = Color.Red)

 - fontSize (글자 크기 지정)

  • 단위: sp (scale-independent Pixels, 가변 크기)
Text(text = "큰 글씨", fontSize = 24.sp)

 - fontWeight (글씨 두께 조절)

  • 사용 가능한 값
    • FontWeight.Normal
    • FontWeight.Bold
    • FontWeight.Light
    • FontWeight.W100 ~ FontWeight.W900 (100~900까지 조정)
Text(text = "굵은 글씨", fontWeight = FontWeight.Bold)


 - fontStyle (이탤릭 적용)

  • FontStyle.Normal
  • FontStyle.Italic
Text(text = "이탤릭 글씨", fontStyle = FontStyle.Italic)

 - textAlign (정렬 설정)

 

  • TextAlign.Left (왼쪽 정렬, 기본값)
  • TextAlign.Center (가운데 정렬)
  • TextAlign.Right (오른쪽 정렬)
  • TextAlign.Justify (양쪽 정렬)
Text(
    text = "가운데 정렬",
    textAlign = TextAlign.Center,
    modifier = Modifier.fillMaxWidth()
)

 

 - maxLines (최대 줄 수 제한)

  • 초과하는 줄은 자동으로 생략
Text(text = "이 텍스트는 너무 깁니다. 잘릴 수도 있습니다.", maxLines = 1)

 - overFlow (텍스트 넘칠 때 처리 방법)

 

  • TextOverflow.Clip (기본값, 단순히 잘림)
  • TextOverflow.Ellipsis (말줄임표 ... 표시)
  • TextOverflow.Visible (넘치는 텍스트 표시)
Text(
    text = "이 텍스트는 길어서 잘릴 수도 있습니다.",
    maxLines = 1,
    overflow = TextOverflow.Ellipsis
)

 

 - letterSpacing (자간 조절)

Text(text = "자 간 넓 히 기", letterSpacing = 4.sp)

 - lineHeight (줄 간격 조절)

Text(text = "첫 번째 줄\n두 번째 줄", lineHeight = 30.sp)

 - shadow (그림자 효과)

Text(
    text = "그림자 효과",
    style = TextStyle(
        shadow = Shadow(color = Color.Gray, offset = Offset(3f, 3f), blurRadius = 5f)
    )
)

 - textDecoration (밑줄, 취소선)

  • TextDecoration.None (기본값)
  • TextDecoration.Underline (밑줄)
  • TextDecoration.LineThrough (취소선)
Text(text = "밑줄", textDecoration = TextDecoration.Underline)
Text(text = "취소선", textDecoration = TextDecoration.LineThrough)

 - style (텍스트 스타일 한번에 적용)

Text(
    text = "스타일 적용",
    style = TextStyle(
        color = Color.Blue,
        fontSize = 20.sp,
        fontWeight = FontWeight.Bold,
        fontStyle = FontStyle.Italic,
        textDecoration = TextDecoration.Underline
    )
)