Jetpack [대왕놀] TextView in Compose (Text)

황제낙엽 2022.12.20 14:37 조회 수 : 236

sitelink1 https://aroundck.tistory.com/8189 
sitelink2  
sitelink3  

Android View 시스템의 TextView 는 Compose 에서 'Text' 에 매핑된다.

 

#

Text 는 아래와 같은 signature 를 갖고 있다.

fun Text(
    text: String,
    modifier: Modifier = Modifier,
    color: Color = Color.Unspecified,
    fontSize: TextUnit = TextUnit.Unspecified,
    fontStyle: FontStyle? = null,
    fontWeight: FontWeight? = null,
    fontFamily: FontFamily? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,
    textDecoration: TextDecoration? = null,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    style: TextStyle = LocalTextStyle.current
)

 

#

Text 에 stringRes 를 지정하려면 stringResource 함수를 활용하자.

Text(
	text=stringResource(R.string.app_name),
)

 

#

Text 의 fontSize 지정은 fontSize parameter 를 활용한다. Int.sp 함수가 쓰인 것을 주목하자.

Text(
	text="Hello World",
	fontSize=18.sp,
)

 

#

Text 에 bold 를 추가하려면 fontWeight parameter 를 활용한다.

Text(
	text="Hello World",
	fontWeight = FontWeight.Bold,
)

FontWeight 에는 Thin, Light, Normal, Medium, Bold 와 함께 추가적인 값들 (ex. ExtraBold) 이 있으니 사용시 참고해서 사용하자.

 

#

Text 에 Android View 시스템의 gravity 값을 주려면, textAlign parameter 를 활용하자.

Text(
	text="Hello World",
	textAlign = TextAlign.End,
)

 

#

Text 를 singleLine 처리 하려면 maxLines parameter 를 활용하자.

Text(
	text="Hello World",
	maxLines=1,
)

 

#

Text 에 ellipsize(말줄임. android:ellipsize) 옵션을 주려면 overflow parameter 를 활용하자.

Text(
	text="Hello World",
	overflow=TextOverflow.Ellipsis,
)

TextOverflow 의 constant 는 Clip(잘라내기, 기본값), Ellipsis, Visible(bounds 를 넘어서도 그리는..) 이 있다.

 

#

Text 에 underline (밑줄) 옵션을 주려면 textDecoration parameter 를 활용하자.

Text(
	text="Hello World",
	textDecoration=TextDecoration.Underline,
)

TextDecoration 의 constant 는 None, Underline, LineThrough(취소선) 이 있다.

 

#

Text 에 italic (기울어진) 옵션을 주려면 fontStyle parameter 를 활용하자.

Text(
	text="Hello World",
	fontStyle=FontStyle.Italic,
)

 

#

Text 에 clickListener 를 주기 위해서는 Modifier.clickable 을 활용하자.

Text(
	text="Hello World",
    modifier=Modifier.clickable { /* .. */ },
)

 

번호 제목 글쓴이 날짜 조회 수
128 단말기 고유값 구하는 방법들 황제낙엽 2019.03.03 11890
127 저장소 파일 불러올 때 권한 요청 설정 file 황제낙엽 2018.08.21 2964
126 icudtl.dat (Microsoft Office Access 2010 14를 위해 Microsoft가 생성한 Dynamic Link Library) 황제낙엽 2021.07.07 2124
125 Emulator: audio: Failed to create voice `adc' 황제낙엽 2018.08.06 1892
124 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-04 file 황제낙엽 2018.08.12 1830
123 고유 식별자의 모범 사례 (Android Developers) 황제낙엽 2019.03.03 1253
122 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-06 file 황제낙엽 2018.08.19 1138
121 HTTP 프로토콜을 이용한 Json Post 보내고 받기 file 황제낙엽 2017.08.03 929
120 Error:Execution failed for task ':app:lintVitalRelease'. 황제낙엽 2018.01.29 922
119 파일 입출력(내장 메모리, 외장메모리) 황제낙엽 2018.08.19 855
118 HttpURLConnection 에서 세션 유지하기(쿠키 사용) 황제낙엽 2017.08.03 790
117 STT 학습 링크 모음 (sample link) 황제낙엽 2018.10.11 780
116 [성공샘플] HttpURLConnection 을 이용하여 JSON 데이터 보내기 예제 황제낙엽 2018.11.10 762
115 Image to byte Array (바로 사용가능한 JPEG 파일) 황제낙엽 2018.07.24 760
114 ABI 관리 황제낙엽 2017.03.28 643
113 TTS 를 위한 스마트폰 설정 및 TTS 샘플 file 황제낙엽 2019.02.16 595
112 안드로이드 스튜디오(Android Studio) 최적화 file 황제낙엽 2018.02.07 575
111 android.webkit.CookieManager 를 이용한 웹뷰와의 세션 공유 황제낙엽 2019.04.26 501
110 동적 레이아웃 생성과 자동 줄바꿈 구현 file 황제낙엽 2018.12.26 446
109 Kotlin의 기본 문법 황제낙엽 2022.11.04 411