๋์์ธ QA๋ฅผ ํ๋์ค ์์คํ ์ค์ ์์ ๊ธ์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ์ ๋ ์์คํ ์ค์ ๊ฐ์ ๋ฐ๋ผ๊ฐ์ง ์๊ณ ๋ชจ๋ ๊ธฐ๊ธฐ ๋๊ฐ์ด ๋ ธ์ถํด๋ฌ๋ผ.
Compose์์ Text์ fontSize๋ฅผ sp๋ก ์ง์ ํ๊ฒ ๋๋ฉด ์์คํ ์ค์ ์ ๋ฐ๋ผ๊ฐ๊ฒ ๋๊ณ dp๋ก ์ค์ ํด์ผ๋ง ์ ๋๊ฐ์ผ๋ก ์ ์ง๋๋ ๊ฒ์ ์์๋ค.
๊ทผ๋ฐ TextStyle๋ก ๋ชจ๋ ๊ณ ์ ์์ผ์ style์ ์ง์ ํด์ฃผ๊ณ ์๋ ๋ dp๋ก ์ค์ ํ๋ ค๊ณ ๋๋ฆฌ๋ฅผ ์น๋ค ๊ฒฐ๊ตญ ์๋์ ๊ฐ์ด ์ค์ ํ๊ฒ ๋๋ค.
์ด๋ฒ์ ๋ฐฐ์ด ๊ฒ!
1. Compose์์๋ fontSize๋ฅผ ์ ํ ๋ sp๋ฅผ ์ ์ธํด์ค์ผํ๋ค.
2. ๊ผญ dp๋ก fontSize๋ฅผ ์ ์ธํด์ค์ผ ํ๋ค๋ฉด, ์๋์ ๊ฐ์ด ์ ์ธ ํด์ฃผ๋ฉด ๋๋ค.
(๋ฌด์กฐ๊ฑด Composable ์ด๋ ธํ ์ด์ ์ ์ถ๊ฐํด์ค์ผ๋ง ๊ฐ๋ฅํ ํจ์์ด๋ค)

3. ํ์ง๋ง ๋์์ด๋์ ์ผํ๋ฉด์ textStyle ๊ฐ์ด ์ ํด์ ธ ์๋ font, fontWeight, fontSize๋ฅผ ์๋ ์ ์ฉ์์ผ์ฃผ๊ณ ์ถ๋ค.
๊ทธ๋์ Compose ์กํฐ๋นํฐ๋ฅผ ์๋ก ๋ง๋ค๋ฉด ์๋์ผ๋ก ์์ฑํด์ฃผ๋ type.kt ํ์ผ์ Typography ๊ฐ ์ ์ธ ๋์ด์๋ค.

์ฌ๊ธฐ๋ฅผ ์ข ๋ฐ๊ฟ์ค ๊ฒ์ด๋ค.
(๊ทธ๋ฅ ๋ฐ๋ก ํ์ผ์ ๋ง๋ค์ด์ค๋ ์๊ด์๋ค!)
1. Int๋ฅผ dp๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ค๋ค.
val Int.nonScaledSp
@Composable
get() = (this / LocalDensity.current.fontScale).sp
// ์ฌ์ฉ์ int๊ฐ ๋ค์ ๋ณ์๋ช
์ ์จ์ฃผ๋ฉด ๋๋ค.
// ex) 15.nonScaledSp, 24.nonScaledSp
2. Enum class๋ฅผ ๋ง๋ค์ด ๋์์ด๋๊ฐ ์ ํ TextStyle์ ์ด๋ฆ์ ์จ์ค๋ค.
enum class TextStyleEnum {
TitleLarge, TitleMedium, TitleSmall, BodyLarge, BodyMedium, BodySmall
// ํ์์ ๋ ์ถ๊ฐํด์ ์ฌ์ฉํ๋ฉด ๋๋ค. ex) content1, subTextSmall
}
3. when๋ฌธ์ด๋ if-else๋ฌธ์ ๋ง๋ค์ด์ ํ์ํ TextUnit( = Typography)์ ๋ง๋ค์ด์ค๋ค.
@Composable
fun typography(textStyle: TextStyleEnum): TextStyle {
return when (textStyle) {
TextStyleEnum.TitleLarge -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 20.nonScaledSp
)
}
TextStyleEnum.TitleMedium -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 17.nonScaledSp
)
}
// ์ค๊ฐ ์๋ต...
TextStyleEnum.BodySmall -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 12.nonScaledSp,
lineHeight = 16.nonScaledSp,
)
}
}
}
4 . ํ์ํ ๊ณณ์ ๋ฃ์ด์ฃผ๋ฉด ๋!
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier,
style = typography(TextStyleEnum.BodyMedium) // <- ์ด๋ฐ์์ผ๋ก ์ฌ์ฉ!!
)
}
5. ํ์ผ ์ ๋ฌธ.
package com.example.blog.ui.theme
import androidx.compose.material3.Typography
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.example.blog.R
// Set of Material typography styles to start with
val Pretendard = FontFamily(
// ๊ฐ์ ์ฌ์ฉํ ํฐํธ๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
Font(R.font.pretendard_regular, FontWeight.Normal, FontStyle.Normal),
Font(R.font.pretendard_bold, FontWeight.Bold, FontStyle.Normal)
)
enum class TextStyleEnum {
TitleLarge, TitleMedium, TitleSmall, BodyLarge, BodyMedium, BodySmall
}
val Int.nonScaledSp
@Composable
get() = (this / LocalDensity.current.fontScale).sp
@Composable
fun typography(textStyle: TextStyleEnum): TextStyle {
return when (textStyle) {
TextStyleEnum.TitleLarge -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 20.nonScaledSp
)
}
TextStyleEnum.TitleMedium -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 17.nonScaledSp
)
}
TextStyleEnum.TitleSmall -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 15.nonScaledSp,
lineHeight = 20.nonScaledSp,
)
}
TextStyleEnum.BodyLarge -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 17.nonScaledSp,
lineHeight = 23.nonScaledSp,
)
}
TextStyleEnum.BodyMedium -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 15.nonScaledSp,
lineHeight = 22.nonScaledSp,
)
}
TextStyleEnum.BodySmall -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 12.nonScaledSp,
lineHeight = 16.nonScaledSp,
)
}
}
}
val Typography = Typography(
// title
titleMedium = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 17.sp
),
titleSmall = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 15.sp,
lineHeight = 20.sp,
),
// body
bodyLarge = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 17.sp,
lineHeight = 23.sp,
),
bodyMedium = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 15.sp,
lineHeight = 22.sp,
),
bodySmall = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 12.sp,
lineHeight = 16.sp,
),
)
xml์ ๋ฒ์ด๋ Compose๋ฅผ ๋์ ํ๊ณ ๋๋ ๊ฐ๋ฐ์ ์ฒ์ ํ๋ ์์ (๊ทธ๋ฆฌ ๋ฉ์ง๋ ์์...)๋ ์๊ฐ๋๊ณ ๋ ๋ฐฐ์ฐ๊ณ ์ฐพ๋ค ๋ณด๋ ๊ฐ๋ฐ์ด ๋ ์ฌ๋ฐ์ด ์ง๊ณ ์๋ค.
์ ์ง/๋ณด์๋ฅผ ํธํ๊ฒ ํ๊ณ ์์ ๋๋๋ผ๋ ์ ์ฐํ๊ฒ ์ ์ฉํ ์ค ์๋ ๊ฐ๋ฐ์๊ฐ ๋๊ธฐ ์ํด ๋ ๋์๊ฐ์ผ๊ฒ ๋ค.
๋
'Development > Compose' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Jetpack Compose Camp 2022 ํ๊ธฐ (0) | 2023.04.05 |
---|---|
๋์ธ๋ผ๋ Compose! ๊ทธ๋ ํ๋ฒ ๋ฐฐ์๋ณผ๊น? ๐ (0) | 2023.04.05 |
๋์์ธ QA๋ฅผ ํ๋์ค ์์คํ ์ค์ ์์ ๊ธ์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ์ ๋ ์์คํ ์ค์ ๊ฐ์ ๋ฐ๋ผ๊ฐ์ง ์๊ณ ๋ชจ๋ ๊ธฐ๊ธฐ ๋๊ฐ์ด ๋ ธ์ถํด๋ฌ๋ผ.
Compose์์ Text์ fontSize๋ฅผ sp๋ก ์ง์ ํ๊ฒ ๋๋ฉด ์์คํ ์ค์ ์ ๋ฐ๋ผ๊ฐ๊ฒ ๋๊ณ dp๋ก ์ค์ ํด์ผ๋ง ์ ๋๊ฐ์ผ๋ก ์ ์ง๋๋ ๊ฒ์ ์์๋ค.
๊ทผ๋ฐ TextStyle๋ก ๋ชจ๋ ๊ณ ์ ์์ผ์ style์ ์ง์ ํด์ฃผ๊ณ ์๋ ๋ dp๋ก ์ค์ ํ๋ ค๊ณ ๋๋ฆฌ๋ฅผ ์น๋ค ๊ฒฐ๊ตญ ์๋์ ๊ฐ์ด ์ค์ ํ๊ฒ ๋๋ค.
์ด๋ฒ์ ๋ฐฐ์ด ๊ฒ!
1. Compose์์๋ fontSize๋ฅผ ์ ํ ๋ sp๋ฅผ ์ ์ธํด์ค์ผํ๋ค.
2. ๊ผญ dp๋ก fontSize๋ฅผ ์ ์ธํด์ค์ผ ํ๋ค๋ฉด, ์๋์ ๊ฐ์ด ์ ์ธ ํด์ฃผ๋ฉด ๋๋ค.
(๋ฌด์กฐ๊ฑด Composable ์ด๋ ธํ ์ด์ ์ ์ถ๊ฐํด์ค์ผ๋ง ๊ฐ๋ฅํ ํจ์์ด๋ค)

3. ํ์ง๋ง ๋์์ด๋์ ์ผํ๋ฉด์ textStyle ๊ฐ์ด ์ ํด์ ธ ์๋ font, fontWeight, fontSize๋ฅผ ์๋ ์ ์ฉ์์ผ์ฃผ๊ณ ์ถ๋ค.
๊ทธ๋์ Compose ์กํฐ๋นํฐ๋ฅผ ์๋ก ๋ง๋ค๋ฉด ์๋์ผ๋ก ์์ฑํด์ฃผ๋ type.kt ํ์ผ์ Typography ๊ฐ ์ ์ธ ๋์ด์๋ค.

์ฌ๊ธฐ๋ฅผ ์ข ๋ฐ๊ฟ์ค ๊ฒ์ด๋ค.
(๊ทธ๋ฅ ๋ฐ๋ก ํ์ผ์ ๋ง๋ค์ด์ค๋ ์๊ด์๋ค!)
1. Int๋ฅผ dp๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ค๋ค.
val Int.nonScaledSp
@Composable
get() = (this / LocalDensity.current.fontScale).sp
// ์ฌ์ฉ์ int๊ฐ ๋ค์ ๋ณ์๋ช
์ ์จ์ฃผ๋ฉด ๋๋ค.
// ex) 15.nonScaledSp, 24.nonScaledSp
2. Enum class๋ฅผ ๋ง๋ค์ด ๋์์ด๋๊ฐ ์ ํ TextStyle์ ์ด๋ฆ์ ์จ์ค๋ค.
enum class TextStyleEnum {
TitleLarge, TitleMedium, TitleSmall, BodyLarge, BodyMedium, BodySmall
// ํ์์ ๋ ์ถ๊ฐํด์ ์ฌ์ฉํ๋ฉด ๋๋ค. ex) content1, subTextSmall
}
3. when๋ฌธ์ด๋ if-else๋ฌธ์ ๋ง๋ค์ด์ ํ์ํ TextUnit( = Typography)์ ๋ง๋ค์ด์ค๋ค.
@Composable
fun typography(textStyle: TextStyleEnum): TextStyle {
return when (textStyle) {
TextStyleEnum.TitleLarge -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 20.nonScaledSp
)
}
TextStyleEnum.TitleMedium -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 17.nonScaledSp
)
}
// ์ค๊ฐ ์๋ต...
TextStyleEnum.BodySmall -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 12.nonScaledSp,
lineHeight = 16.nonScaledSp,
)
}
}
}
4 . ํ์ํ ๊ณณ์ ๋ฃ์ด์ฃผ๋ฉด ๋!
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier,
style = typography(TextStyleEnum.BodyMedium) // <- ์ด๋ฐ์์ผ๋ก ์ฌ์ฉ!!
)
}
5. ํ์ผ ์ ๋ฌธ.
package com.example.blog.ui.theme
import androidx.compose.material3.Typography
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.example.blog.R
// Set of Material typography styles to start with
val Pretendard = FontFamily(
// ๊ฐ์ ์ฌ์ฉํ ํฐํธ๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
Font(R.font.pretendard_regular, FontWeight.Normal, FontStyle.Normal),
Font(R.font.pretendard_bold, FontWeight.Bold, FontStyle.Normal)
)
enum class TextStyleEnum {
TitleLarge, TitleMedium, TitleSmall, BodyLarge, BodyMedium, BodySmall
}
val Int.nonScaledSp
@Composable
get() = (this / LocalDensity.current.fontScale).sp
@Composable
fun typography(textStyle: TextStyleEnum): TextStyle {
return when (textStyle) {
TextStyleEnum.TitleLarge -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 20.nonScaledSp
)
}
TextStyleEnum.TitleMedium -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 17.nonScaledSp
)
}
TextStyleEnum.TitleSmall -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 15.nonScaledSp,
lineHeight = 20.nonScaledSp,
)
}
TextStyleEnum.BodyLarge -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 17.nonScaledSp,
lineHeight = 23.nonScaledSp,
)
}
TextStyleEnum.BodyMedium -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 15.nonScaledSp,
lineHeight = 22.nonScaledSp,
)
}
TextStyleEnum.BodySmall -> {
TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 12.nonScaledSp,
lineHeight = 16.nonScaledSp,
)
}
}
}
val Typography = Typography(
// title
titleMedium = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 17.sp
),
titleSmall = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Bold,
fontSize = 15.sp,
lineHeight = 20.sp,
),
// body
bodyLarge = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 17.sp,
lineHeight = 23.sp,
),
bodyMedium = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 15.sp,
lineHeight = 22.sp,
),
bodySmall = TextStyle(
fontFamily = Pretendard,
fontWeight = FontWeight.Normal,
fontSize = 12.sp,
lineHeight = 16.sp,
),
)
xml์ ๋ฒ์ด๋ Compose๋ฅผ ๋์ ํ๊ณ ๋๋ ๊ฐ๋ฐ์ ์ฒ์ ํ๋ ์์ (๊ทธ๋ฆฌ ๋ฉ์ง๋ ์์...)๋ ์๊ฐ๋๊ณ ๋ ๋ฐฐ์ฐ๊ณ ์ฐพ๋ค ๋ณด๋ ๊ฐ๋ฐ์ด ๋ ์ฌ๋ฐ์ด ์ง๊ณ ์๋ค.
์ ์ง/๋ณด์๋ฅผ ํธํ๊ฒ ํ๊ณ ์์ ๋๋๋ผ๋ ์ ์ฐํ๊ฒ ์ ์ฉํ ์ค ์๋ ๊ฐ๋ฐ์๊ฐ ๋๊ธฐ ์ํด ๋ ๋์๊ฐ์ผ๊ฒ ๋ค.
๋
'Development > Compose' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Jetpack Compose Camp 2022 ํ๊ธฐ (0) | 2023.04.05 |
---|---|
๋์ธ๋ผ๋ Compose! ๊ทธ๋ ํ๋ฒ ๋ฐฐ์๋ณผ๊น? ๐ (0) | 2023.04.05 |