Text Composable in Jetpack Compose
Hello guys,๐
If you have yet not setup Basic Compose project, please jump for 1st day of learning for Introduction to Compose & Setup ๐๏ธ
Introduction to Text in Compose
Text is one of the most important & central element in UI design. Everything starts from text and end with some complex elements. In the development process, requirements are always not satisfied with normal element but there may arise a need to work on the complex form of Text & other elements.
Compose provides a BasicText
and BasicTextField
which are the essential elements to display text and handle user input. Also, Compose provides Text
and TextField
, which are composable following Material Design guidelines. It is preferred to use those as they provide a good look & feel and allow customisation in less code.
Basics to display text using Composable is ๐
Most of the times it is recommended to use Strings from String.xml resource file that can be achieved using
If we go in some more deep attributes the first we can think of is Styling a Text with (colour, size, style, background, alignment, font etc)
1. Text Colour
2. Text Size
3. Text Style (Italic, Bold)
4. Text Alignment
5. Text Font
Text has a Font family parameter to set to text which is used by composable. By default, serif, sans-serif, monospace and cursive font families are included. We can put files in font resource folder of Android and set font family by referencing from it
Going one more level deep for Multiple Style for Text
If we want to set different styles within the same text, we have to use an AnnotatedString
, a string that can be annotated with styles of arbitrary annotations.AnnotatedString
is a data class containing:
- A
Text
value - A
List
ofSpanStyleRange
, for inline styling with position range within the text value - A
List
ofParagraphStyleRange
, for text alignment, text direction, line height, and text-indent styling
Note :
TextStyle
is for use in theText
composable , whereasSpanStyle
andParagraphStyle
is for use inAnnotatedString
.Note : Difference between
SpanStyle
andParagraphStyle
is thatParagraphStyle
can be applied to a whole paragraph,SpanStyle
can be applied at the character level.
Oops, itโs was all about text Displaying text. But what about Entering the Text? ๐ค๐ค
TextField
allows users to enter and modify text. There are two levels of TextField
implementations:
TextField
is the Material Design implementation.
2. BasicTextField
enables users to edit text via hardware or software keyboard, but provides no decorations like hint or placeholder
Thatโs all about the basics of Text. This article may not cover everything based on your complex Implementation & use cases but you are good to start with. All the best.๐
You can Clone this repository for basic setup. Further, all concepts source code can be updated from same. (Switch to branch TextCompose)
https://github.com/SatyamGondhale/LearnCompose
If you think this has helped you, feel free to ๐๐ป (claps) & share. Thanks.๐