Rich text for android textview.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
gradle/wrapper
richtext
.gitignore
README.md
RichText.gif
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

RichText

Release

This library parses string with custom tags to Spannable string.

Features

  • hyperlink click event (with pressed text and background color)
  • click event (with pressed text and background color)
  • text background color
  • text foreground color
  • text size
  • text style (bold, italic)
  • text font
  • image

Of course, you can add custom typeface span with api addTypeSpan.

Installation

In your project level build.gradle :

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

In your app level build.gradle :

dependencies {
    compile 'com.github.xymelon:richtext:1.0.10'
}

Usage with custom tags

TextView textView = (TextView) findViewById(R.id.textView);

String tagString = "The <a href='https://en.wikipedia.org/wiki/Rich_Text_Format'>Rich Text Format</a> " +
        "is a <c>proprietary</c> <f>document</f> file format with published <bi>specification</bi> " +
        "developed by <t>Microsoft Corporation</t> from 1987 until 2008 for <s>cross-platform</s> document interchange " +
        "with Microsoft products. <img src='ic_vip' />";

final int foregroundTextColor = ContextCompat.getColor(this, R.color.T1);
final int linkTextColor = ContextCompat.getColor(this, R.color.colorPrimary);
final int normalTextColor = ContextCompat.getColor(this, R.color.R1);
final int pressedTextColor = ContextCompat.getColor(this, R.color.W1);
final int pressedBackgroundColor = ContextCompat.getColor(this, R.color.B1);
final Typeface georgiaTypeface = Typeface.createFromAsset(getAssets(), "fonts/Georgia Italic.ttf");

RichText richText = new RichText.Builder()
        .addBlockTypeSpan(new ClickSpan(
                normalTextColor,
                pressedTextColor,
                pressedBackgroundColor,
                new ClickSpan.OnClickListener() {
                    @Override
                    public void onClick(TextView textView, CharSequence text, float rawX, float rawY) {
                        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
                    }
                }), "c")
        .addBlockTypeSpan(new IStyleSpan() {
            @Override
            public CharacterStyle getStyleSpan() {
                return new ForegroundColorSpan(foregroundTextColor);
            }
        }, "f", "t")
        .addBlockTypeSpan(new IStyleSpan() {
            @Override
            public CharacterStyle getStyleSpan() {
                return new StyleSpan(Typeface.BOLD_ITALIC);
            }
        }, "bi")
        .addBlockTypeSpan(new IStyleSpan() {
            @Override
            public CharacterStyle getStyleSpan() {
                return new TextAppearanceSpan(MainActivity.this, R.style.TextSize);
            }
        }, "s")
        .addBlockTypeSpan(new FontTypefaceSpan(georgiaTypeface), "t")
        .addLinkTypeSpan(new LinkClickSpan(
                linkTextColor,
                pressedTextColor,
                pressedBackgroundColor,
                new LinkClickSpan.OnLinkClickListener() {
                    @Override
                    public void onClick(TextView textView, String url) {
                        Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show();
                    }
                })
        )
        .addImageSpan(new ImageSpanGetter() {
            @Override
            public ImageSpan getImageSpan(String src) {
                final Drawable drawable = getDrawable(textView.getContext(), src);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return new CenteredImageSpan(drawable);
            }
        })
        .build();
//notice: if set click span, you must invoke this method.
richText.with(textView);
textView.setText(richText.parse(tagString));

License

Copyright 2017 xymelon.

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.