1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.ui.text.samples
18
19 import androidx.annotation.Sampled
20 import androidx.compose.material.Text
21 import androidx.compose.runtime.Composable
22 import androidx.compose.ui.text.ParagraphStyle
23 import androidx.compose.ui.text.TextStyle
24 import androidx.compose.ui.text.buildAnnotatedString
25 import androidx.compose.ui.text.style.TextAlign
26 import androidx.compose.ui.text.style.TextIndent
27 import androidx.compose.ui.unit.sp
28
29 @Sampled
30 @Composable
ParagraphStyleSamplenull31 fun ParagraphStyleSample() {
32 val textStyle =
33 TextStyle(
34 textAlign = TextAlign.Justify,
35 lineHeight = 20.sp,
36 textIndent = TextIndent(firstLine = 14.sp, restLine = 3.sp)
37 )
38 Text(
39 text =
40 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " +
41 "incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis " +
42 "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
43 style = textStyle
44 )
45 }
46
47 @Sampled
48 @Composable
ParagraphStyleAnnotatedStringsSamplenull49 fun ParagraphStyleAnnotatedStringsSample() {
50 val text =
51 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " +
52 "incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis " +
53 "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
54
55 val paragraphStyle1 = ParagraphStyle(textIndent = TextIndent(firstLine = 14.sp))
56 val paragraphStyle2 = ParagraphStyle(lineHeight = 30.sp)
57
58 Text(
59 text =
60 buildAnnotatedString {
61 append(text)
62 addStyle(paragraphStyle1, 0, text.indexOf('\n') + 1)
63 addStyle(paragraphStyle2, text.indexOf('\n') + 1, text.length)
64 }
65 )
66 }
67