• Stars
    star
    216
  • Rank 183,179 (Top 4 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

💯ㅤA SpannableString Parser for Android

SpannableStringParser

CircleCI Codacy Badge codecov
Android Weekly

In Android, to style our text we use Spans.
Spans are markup objects that can be applied to parts of the text.

It requires us to deal with the indexes of the text that we want to style.

val string = SpannableString("Text with a foreground color span")
string.setSpan(ForegroundColorSpan(Color.RED), 12, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

But what if the text is dynamic and the styling of the text is also dynamic.
Now, dealing with indexes and styling becomes really difficult for us.

This is where SpannableStringParser helps us.
You can specify which text to style and how to style them inside the string itself, and use the spannify() extension function to style it.

val string = "Text with a { `foreground color` < text-color:#FF0000 /> } span".spannify()

This string can also come from a back-end server and if we wish to change the style of the text in future, the string can be easily changed from back-end and no front-end change will be required.

SpannableStringParser internally uses Spannable, which is more performant than Html.

Have a Look

1. Text having a property

"Hello { `SpannableStringParser` < text-color:#0000FF /> }"

2. Text having multiple properties

"Hello { `SpannableStringParser` < text-color:#0000FF ; text-decoration:underline /> }"

3. Text having multiple properties with multiple values

"Hello { `SpannableStringParser` < text-color:#0000FF ; text-decoration:underline|strike-through /> }"

Usage

  • Syntax for "text" having a property
text_view.text = "{ `text` < property:value /> }".spannify()
  • Syntax for "text" having multiple properties
text_view.text = "{ `text` < property:value ; property:value /> }".spannify()
  • Syntax for "text" having multiple properties with multiple values
text_view.text = "{ `text` < property:value ; property:value|value /> }".spannify()
  • You can even add custom properties using the spanner method.
spanner { property, value ->
    when (property) {
        "..." -> {
            if (value == "...")
                return@spanner Custom1Span()
        }
        "..." -> {
            if (value == "...")
                return@spanner Custom2Span()
        }
    }
    return@spanner null
}
  • If you don't want these properties and values coming from a back-end server, you can use the property method.
"text"
    .property("property", "value")
    .property("property", "value", "value")
    .spannify()

Note: Make sure to call the spannify method at the end.


If you are using Node.js for your back-end, you can use SpannableStringFormatter to efficiently and correctly format your strings.

Supported Properties

text-color

  • The text-color property specifies the color of text.
"Hello { `World` < text-color:#FF00FF /> }"

"Hello { `World` < text-color:#44FF00FF /> }"


background-color

  • The background-color property specifies the background color of text.
"Hello { `World` < background-color:#FF00FF /> }"

"Hello { `World` < background-color:#44FF00FF /> }"


line-background-color

  • The line-background-color property specifies the background color of lines.
"Hello { `World` < line-background-color:#FF00FF /> }"

"Hello { `World` < line-background-color:#44FF00FF /> }"


text-size

  • The text-size property sets the size of text.
"Hello { `World` < text-size:24dp /> }"

"Hello { `World` < text-size:2.4em /> }"

"Hello { `World` < text-size:24px /> }"


text-decoration

  • The text-decoration property sets the kind of text decoration to use (like underline, strike-through).
"Hello { `World` < text-decoration:underline /> }"

"Hello { `World` < text-decoration:strike-through /> }"


subscript

  • The subscript property defines subscript text.
"Hello { `World` < subscript:true /> }"


superscript

  • The superscript property defines superscript text.
"Hello { `World` < superscript:true /> }"


text-style

  • The text-style property specifies the style of text.
"Hello { `World` < text-style:normal /> }"

"Hello { `World` < text-style:bold /> }"

"Hello { `World` < text-style:italic /> }"


font-family

  • The font-family property specifies the font of text.
"Hello { `World` < font-family:monospace /> }"

"Hello { `World` < font-family:serif /> }"

"Hello { `World` < font-family:sans-serif /> }"


text-alignment

  • The text-alignment property specifies the alignment of text.
"{ `Hello World` < text-alignment:normal /> }"

"{ `Hello World` < text-alignment:opposite /> }"

"{ `Hello World` < text-alignment:center /> }"


line-height

  • The line-height property specifies the height of line.
val lorem_ipsum = "lorem ipsum ..."
"{ `${lorem_ipsum}` < line-height:120px /> }"


url

  • The url property specifies the url for text, clicking on which will open the url.
"Click { `here` < url:`https://www.google.com` /> } for more information"

Note: You have to set LinkMovementMethod on the TextView for supporting url clicks.

text_view.movementMethod = LinkMovementMethod.getInstance()

more-properties-coming-soon

You can add your custom properties using the spanner method.
But! If you want any property to be added in SpannableStringParser, feel free to open issues/pull requests.

Download

Add JitPack repository to your root build.gradle file

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency to your app build.gradle file

dependencies {
    implementation 'com.github.hitanshu-dhawan:SpannableStringParser:1.2.1'
}

Licence

Copyright (c) 2019 Hitanshu Dhawan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

McCompose

🍔ㅤA McDonald’s app built with Jetpack Compose
Kotlin
361
star
2

FirebaseMLKit

📈ㅤ[ARTICLE] Firebase ML Kit 101 Series
Kotlin
108
star
3

PopCorn

🍿ㅤMovies and TV Shows App powered by TMDb
Java
87
star
4

ImageSteganography

🔐ㅤA C++ program for Image Steganography
C++
70
star
5

AnnotationProcessing

✔️ㅤ[ARTICLE] Writing your own Annotation Processors in Android
Java
50
star
6

JsonDSL

📃ㅤJson DSL in Kotlin
Kotlin
44
star
7

CircularProgressBar-Compose

⭕ㅤCircular ProgressBar for Jetpack Compose
Kotlin
43
star
8

PopCorn2

🍿✌️ [APP] Movies and TV Shows App powered by TMDb - In progress
Kotlin
20
star
9

Todo

✏️ㅤSimple Todo App to demonstrate Content Providers, Cursor Loaders, Widgets, Notifications etc.
Java
16
star
10

TicTacToeAI

#️⃣ㅤA C program for a game of TicTacToe using Artificial Intelligence
C
15
star
11

PojoPreferences

💾 [LIBRARY] An android library for storing Pojo variables into SharedPreferences easily
Java
13
star
12

LiveData

🔴ㅤ[ARTICLE] How I made my own LiveData!
Kotlin
11
star
13

SharedPref

💾 [LIBRARY] An android library for making SharedPreferences easy
Java
7
star
14

CustomLintRules

Kotlin
7
star
15

10PRINT

❎ㅤ10 PRINT CHR$(205.5+RND(1)); : GOTO 10
JavaScript
6
star
16

SudokuSolver

🔢ㅤA C program for solving sudoku puzzles
C
6
star
17

BatteryBeep

🔌 💯ㅤA python script to play a beep sound when laptop battery gets fully charged
Python
5
star
18

DynamicWallpapers

🌙 ☀️ㅤA python script that automatically changes desktop wallpaper based on the time of day
Python
5
star
19

BrainfuckCompiler

🖕ㅤBrainfuck Compiler written in C++
C++
5
star
20

SpannableStringFormatter

💯ㅤA SpannableString Formatter for JavaScript
TypeScript
4
star
21

SortingVisualizations

📊ㅤAwesome Sorting Visualizations
JavaScript
2
star
22

TapTap

🎶ㅤShitty clone of patatap.com
JavaScript
2
star
23

hitanshu-dhawan.github.io

:octocat:ㅤMy personal website
HTML
1
star
24

Hackattic

Solutions for Hackattic challenges
1
star