Giter Club home page Giter Club logo

spannablestringparser's Introduction

SpannableStringParser

CircleCI Codacy Badge codecov

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` < 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` < color:#0000FF /> }"

2. Text having multiple properties

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

3. Text having multiple properties with multiple values

"Hello { `SpannableStringParser` < color:#0000FF ; text-decoration-line:underline|line-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) {
        "sub-script" -> if (value == "true") {
            return@spanner SubscriptSpan()
        }
        "super-script" -> if (value == "true") {
            return@spanner SuperscriptSpan()
        }
    }
    return@spanner null
}

Supported Properties

color

The color property specifies the color of text.

Examples: color : #FF00FF , color : #00FF00FF

text-decoration-line

The text-decoration-line property sets the kind of text decoration to use (like underline, line-through).

Examples: text-decoration-line : underline , text-decoration-line : line-through

font-size

The font-size property sets the size of a font.

Examples: font-size : 12dp , font-size : 1.5em , font-size : 24px

font-style

The font-style property specifies the font style for a text.

Examples: font-style : italic

font-weight

The font-weight property sets how thick or thin characters in text should be displayed.

Examples: font-weight : bold

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.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.

spannablestringparser's People

Contributors

hitanshu-dhawan avatar annshsingh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.