1 /*
2 * Copyright 2021 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.foundation.background
21 import androidx.compose.foundation.clickable
22 import androidx.compose.foundation.layout.Box
23 import androidx.compose.foundation.layout.heightIn
24 import androidx.compose.foundation.layout.size
25 import androidx.compose.foundation.layout.width
26 import androidx.compose.material.Text
27 import androidx.compose.runtime.Composable
28 import androidx.compose.runtime.mutableStateOf
29 import androidx.compose.runtime.remember
30 import androidx.compose.ui.Modifier
31 import androidx.compose.ui.graphics.Color
32 import androidx.compose.ui.text.style.TextOverflow
33 import androidx.compose.ui.unit.dp
34 import androidx.compose.ui.unit.sp
35
36 @Sampled
37 @Composable
TextOverflowClipSamplenull38 fun TextOverflowClipSample() {
39 Text(
40 text = "Hello ".repeat(2),
41 modifier = Modifier.size(100.dp, 70.dp).background(Color.Cyan),
42 fontSize = 35.sp,
43 overflow = TextOverflow.Clip
44 )
45 }
46
47 @Sampled
48 @Composable
TextOverflowEllipsisSamplenull49 fun TextOverflowEllipsisSample() {
50 Text(
51 text = "Hello ".repeat(2),
52 modifier = Modifier.width(100.dp).background(Color.Cyan),
53 fontSize = 35.sp,
54 overflow = TextOverflow.Ellipsis,
55 maxLines = 1
56 )
57 }
58
59 @Sampled
60 @Composable
TextOverflowVisibleFixedSizeSamplenull61 fun TextOverflowVisibleFixedSizeSample() {
62 val background = remember { mutableStateOf(Color.Cyan) }
63 Box(modifier = Modifier.size(100.dp, 100.dp)) {
64 Text(
65 text = "Hello ".repeat(2),
66 modifier =
67 Modifier.size(100.dp, 70.dp).background(background.value).clickable {
68 background.value =
69 if (background.value == Color.Cyan) {
70 Color.Gray
71 } else {
72 Color.Cyan
73 }
74 },
75 fontSize = 35.sp,
76 overflow = TextOverflow.Visible
77 )
78 }
79 }
80
81 @Sampled
82 @Composable
TextOverflowVisibleMinHeightSamplenull83 fun TextOverflowVisibleMinHeightSample() {
84 val background = remember { mutableStateOf(Color.Cyan) }
85 val count = remember { mutableStateOf(1) }
86 Box(modifier = Modifier.size(100.dp, 100.dp)) {
87 Text(
88 text = "Hello".repeat(count.value),
89 modifier =
90 Modifier.width(100.dp)
91 .heightIn(min = 70.dp)
92 .background(background.value)
93 .clickable {
94 background.value =
95 if (background.value == Color.Cyan) Color.Gray else Color.Cyan
96 count.value = if (count.value == 1) 2 else 1
97 },
98 fontSize = 35.sp,
99 overflow = TextOverflow.Visible
100 )
101 }
102 }
103