Класс ParagraphStyle
Класс ParagraphStyle описывает
оформление абзаца внутри
AnnotatedString:
textAlign, lineHeight
и направление текста.
Его включают через
pushStyle у builder
или в withStyle при сборке строки.
Для простого Text
часто хватает класса
TextStyle.
Синтаксис
ParagraphStyle(textAlign = TextAlign.Center)
Пример
Давайте выровняем абзац по центру:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val annotated = buildAnnotatedString {
withStyle(ParagraphStyle(textAlign = TextAlign.Center)) {
append("abcde")
}
}
Text(
text = annotated,
modifier = Modifier.fillMaxWidth(),
)
}
}
}
На экране строка "abcde"
встанет по центру ширины текста.
Смотрите также
-
перечисление
TextAlign,
которое выбирает выравнивание строк -
класс
TextStyle,
который задаёт общий стиль для Text -
класс
SpanStyle,
который оформляет отдельный фрагмент -
класс
AnnotatedString,
куда попадает стиль абзаца