Skip to content

πŸ“œπŸ” [LIBRARY] A SpannableString Parser for Android

License

Notifications You must be signed in to change notification settings

urbanclap-engg/SpannableStringParser

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SpannableStringParser

CircleCI 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()

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"

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.urbanclap-engg.SpannableStringParser:spanner:2.0.0'
}

Parser

If your project only requires the core parsing of the string and not the inbuilt spans, you can use the parser submodule.

dependencies {
    implementation 'com.github.urbanclap-engg.SpannableStringParser:parser:2.0.0'
}

If you want both the functionalities in a single project you can add both the dependencies.

dependencies {
    implementation 'com.github.urbanclap-engg.SpannableStringParser:parser:2.0.0'
    implementation 'com.github.urbanclap-engg.SpannableStringParser:spanner:2.0.0'
}

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.

About

πŸ“œπŸ” [LIBRARY] A SpannableString Parser for Android

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 100.0%