Skip to content

Commit be01a96

Browse files
authored
Fix text snippets in latest (#326)
* Fix text snippets in latest * Apply Spotless
1 parent bbdb61b commit be01a96

File tree

1 file changed

+191
-51
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/text

1 file changed

+191
-51
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt

Lines changed: 191 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@
1818

1919
package com.example.compose.snippets.text
2020

21-
import android.util.Log
21+
import androidx.compose.foundation.BorderStroke
22+
import androidx.compose.foundation.background
23+
import androidx.compose.foundation.basicMarquee
24+
import androidx.compose.foundation.border
25+
import androidx.compose.foundation.layout.Arrangement
26+
import androidx.compose.foundation.layout.Box
2227
import androidx.compose.foundation.layout.Column
28+
import androidx.compose.foundation.layout.Row
29+
import androidx.compose.foundation.layout.fillMaxWidth
2330
import androidx.compose.foundation.layout.padding
2431
import androidx.compose.foundation.layout.width
25-
import androidx.compose.foundation.text.ClickableText
2632
import androidx.compose.foundation.text.KeyboardOptions
2733
import androidx.compose.foundation.text.selection.DisableSelection
2834
import androidx.compose.foundation.text.selection.SelectionContainer
2935
import androidx.compose.material3.LocalTextStyle
36+
import androidx.compose.material3.MaterialTheme
3037
import androidx.compose.material3.OutlinedTextField
3138
import androidx.compose.material3.Text
3239
import androidx.compose.material3.TextField
@@ -36,6 +43,7 @@ import androidx.compose.runtime.mutableStateOf
3643
import androidx.compose.runtime.remember
3744
import androidx.compose.runtime.saveable.rememberSaveable
3845
import androidx.compose.runtime.setValue
46+
import androidx.compose.ui.Alignment
3947
import androidx.compose.ui.Modifier
4048
import androidx.compose.ui.geometry.Offset
4149
import androidx.compose.ui.graphics.Brush
@@ -44,7 +52,6 @@ import androidx.compose.ui.graphics.Color.Companion.Cyan
4452
import androidx.compose.ui.graphics.Shadow
4553
import androidx.compose.ui.platform.LocalUriHandler
4654
import androidx.compose.ui.res.stringResource
47-
import androidx.compose.ui.text.AnnotatedString
4855
import androidx.compose.ui.text.LinkAnnotation
4956
import androidx.compose.ui.text.ParagraphStyle
5057
import androidx.compose.ui.text.PlatformTextStyle
@@ -58,11 +65,14 @@ import androidx.compose.ui.text.font.FontStyle
5865
import androidx.compose.ui.text.font.FontWeight
5966
import androidx.compose.ui.text.input.KeyboardType
6067
import androidx.compose.ui.text.input.PasswordVisualTransformation
68+
import androidx.compose.ui.text.style.Hyphens
69+
import androidx.compose.ui.text.style.LineBreak
6170
import androidx.compose.ui.text.style.LineHeightStyle
6271
import androidx.compose.ui.text.style.TextAlign
6372
import androidx.compose.ui.text.style.TextOverflow
6473
import androidx.compose.ui.text.withLink
6574
import androidx.compose.ui.text.withStyle
75+
import androidx.compose.ui.tooling.preview.Preview
6676
import androidx.compose.ui.unit.dp
6777
import androidx.compose.ui.unit.em
6878
import androidx.compose.ui.unit.sp
@@ -407,54 +417,6 @@ private object TextPartiallySelectableSnippet {
407417
// [END android_compose_text_partial_selection]
408418
}
409419

410-
private object TextClickableSnippet {
411-
// [START android_compose_text_clickable]
412-
@Composable
413-
fun SimpleClickableText() {
414-
ClickableText(text = AnnotatedString("Click Me"), onClick = { offset ->
415-
Log.d("ClickableText", "$offset -th character is clicked.")
416-
})
417-
}
418-
// [END android_compose_text_clickable]
419-
}
420-
421-
private object TextClickableAnnotatedSnippet {
422-
// [START android_compose_text_clickable_annotated]
423-
@Composable
424-
fun AnnotatedClickableText() {
425-
val annotatedText = buildAnnotatedString {
426-
append("Click ")
427-
428-
// We attach this *URL* annotation to the following content
429-
// until `pop()` is called
430-
pushStringAnnotation(
431-
tag = "URL", annotation = "https://developer.android.com"
432-
)
433-
withStyle(
434-
style = SpanStyle(
435-
color = Color.Blue, fontWeight = FontWeight.Bold
436-
)
437-
) {
438-
append("here")
439-
}
440-
441-
pop()
442-
}
443-
444-
ClickableText(text = annotatedText, onClick = { offset ->
445-
// We check if there is an *URL* annotation attached to the text
446-
// at the clicked position
447-
annotatedText.getStringAnnotations(
448-
tag = "URL", start = offset, end = offset
449-
).firstOrNull()?.let { annotation ->
450-
// If yes, we log its value
451-
Log.d("Clicked URL", annotation.item)
452-
}
453-
})
454-
}
455-
// [END android_compose_text_clickable_annotated]
456-
}
457-
458420
private object TextTextFieldSnippet {
459421
// [START android_compose_text_textfield_filled]
460422
@Composable
@@ -625,6 +587,184 @@ fun AnnotatedStringWithListenerSample() {
625587
}
626588
// [END android_compose_text_link_2]
627589

590+
@Composable
591+
private fun TextSample(samples: Map<String, @Composable ()->Unit>) {
592+
MaterialTheme {
593+
Box(
594+
Modifier
595+
.background(MaterialTheme.colorScheme.background)
596+
.fillMaxWidth()
597+
) {
598+
Column(
599+
verticalArrangement = Arrangement.spacedBy(10.dp),
600+
modifier = Modifier.padding(10.dp)
601+
) {
602+
samples.forEach { (title, content) ->
603+
Row(Modifier.fillMaxWidth()) {
604+
content()
605+
Text(
606+
text = title,
607+
textAlign = TextAlign.Center,
608+
style = MaterialTheme.typography.titleLarge,
609+
modifier = Modifier
610+
.fillMaxWidth()
611+
.align(Alignment.CenterVertically)
612+
)
613+
}
614+
}
615+
}
616+
}
617+
}
618+
}
619+
620+
private const val SAMPLE_LONG_TEXT =
621+
"Jetpack Compose is Android’s modern toolkit for building native UI. " +
622+
"It simplifies and accelerates UI development on Android bringing your apps " +
623+
"to life with less code, powerful tools, and intuitive Kotlin APIs. " +
624+
"It makes building Android UI faster and easier."
625+
@Composable
626+
@Preview
627+
fun LineBreakSample() {
628+
// [START android_compose_text_line_break]
629+
TextSample(
630+
samples = mapOf(
631+
"Simple" to {
632+
Text(
633+
text = SAMPLE_LONG_TEXT,
634+
modifier = Modifier
635+
.width(130.dp)
636+
.border(BorderStroke(1.dp, Color.Gray)),
637+
fontSize = 14.sp,
638+
style = TextStyle.Default.copy(
639+
lineBreak = LineBreak.Simple
640+
)
641+
)
642+
},
643+
"Paragraph" to {
644+
Text(
645+
text = SAMPLE_LONG_TEXT,
646+
modifier = Modifier
647+
.width(130.dp)
648+
.border(BorderStroke(1.dp, Color.Gray)),
649+
fontSize = 14.sp,
650+
style = TextStyle.Default.copy(
651+
lineBreak = LineBreak.Paragraph
652+
)
653+
)
654+
}
655+
)
656+
)
657+
// [END android_compose_text_line_break]
658+
}
659+
660+
@Preview
661+
@Composable
662+
fun SmallScreenTextSnippet() {
663+
// [START android_compose_text_paragraph]
664+
TextSample(
665+
samples = mapOf(
666+
"Balanced" to {
667+
val smallScreenAdaptedParagraph =
668+
LineBreak.Paragraph.copy(strategy = LineBreak.Strategy.Balanced)
669+
Text(
670+
text = SAMPLE_LONG_TEXT,
671+
modifier = Modifier
672+
.width(200.dp)
673+
.border(BorderStroke(1.dp, Color.Gray)),
674+
fontSize = 14.sp,
675+
style = TextStyle.Default.copy(
676+
lineBreak = smallScreenAdaptedParagraph
677+
)
678+
)
679+
},
680+
"Default" to {
681+
Text(
682+
text = SAMPLE_LONG_TEXT,
683+
modifier = Modifier
684+
.width(200.dp)
685+
.border(BorderStroke(1.dp, Color.Gray)),
686+
fontSize = 14.sp,
687+
style = TextStyle.Default
688+
)
689+
}
690+
)
691+
)
692+
// [END android_compose_text_paragraph]
693+
}
694+
695+
private object CJKTextSnippet {
696+
@Composable
697+
fun CJKSample() {
698+
// [START android_compose_text_cjk]
699+
val customTitleLineBreak = LineBreak(
700+
strategy = LineBreak.Strategy.HighQuality,
701+
strictness = LineBreak.Strictness.Strict,
702+
wordBreak = LineBreak.WordBreak.Phrase
703+
)
704+
Text(
705+
text = "あなたに寄り添う最先端のテクノロジー。",
706+
modifier = Modifier.width(250.dp),
707+
fontSize = 14.sp,
708+
style = TextStyle.Default.copy(
709+
lineBreak = customTitleLineBreak
710+
)
711+
)
712+
// [END android_compose_text_cjk]
713+
}
714+
}
715+
716+
@Preview
717+
@Composable
718+
fun HyphenateTextSnippet() {
719+
// [START android_compose_text_hyphen]
720+
TextSample(
721+
samples = mapOf(
722+
"Hyphens - None" to {
723+
Text(
724+
text = SAMPLE_LONG_TEXT,
725+
modifier = Modifier
726+
.width(130.dp)
727+
.border(BorderStroke(1.dp, Color.Gray)),
728+
fontSize = 14.sp,
729+
style = TextStyle.Default.copy(
730+
lineBreak = LineBreak.Paragraph,
731+
hyphens = Hyphens.None
732+
)
733+
)
734+
},
735+
"Hyphens - Auto" to {
736+
Text(
737+
text = SAMPLE_LONG_TEXT,
738+
modifier = Modifier
739+
.width(130.dp)
740+
.border(BorderStroke(1.dp, Color.Gray)),
741+
fontSize = 14.sp,
742+
style = TextStyle.Default.copy(
743+
lineBreak = LineBreak.Paragraph,
744+
hyphens = Hyphens.Auto
745+
)
746+
)
747+
}
748+
)
749+
)
750+
// [END android_compose_text_hyphen]
751+
}
752+
753+
@Preview(showBackground = true)
754+
// [START android_compose_text_marquee]
755+
@Composable
756+
fun BasicMarqueeSample() {
757+
// Marquee only animates when the content doesn't fit in the max width.
758+
Column(Modifier.width(400.dp)) {
759+
Text(
760+
"Learn about why it's great to use Jetpack Compose",
761+
modifier = Modifier.basicMarquee(),
762+
fontSize = 50.sp
763+
)
764+
}
765+
}
766+
// [END android_compose_text_marquee]
767+
628768
private val firaSansFamily = FontFamily()
629769

630770
val LightBlue = Color(0xFF0066FF)

0 commit comments

Comments
 (0)