Git Product home page Git Product logo

rich-text-android's Introduction

Notice

This fork does not follow base repo, I simply add what i think is missing or adding features that i need in my app.

RichText Library

Download

Features

  • Display rich text using a fluent interface.
  • Support Image loading from network.
  • Parse HTML, including images and video tag into displayable content.

Download

Use Gradle:

repositories {
   jcenter()
}

dependencies {
  compile 'io.square1:richtext:x.x.x'
}

Setup a RichContentView

Add a RichContentView to an xml layout, wrap around a ScrollView to enable content scrolling.

 <ScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     <io.square1.richtextlib.ui.RichContentView
         android:padding="10dip"
         android:id="@+id/richTextView"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/hello_blank_fragment" />
 </ScrollView>

Enable Image download

Supply an instance of a class that implements UrlImageDownloader instance and can download images from the network:

    contentView.setUrlBitmapDownloader(new UrlBitmapDownloader() {
      @Override
      public void downloadImage(final RemoteBitmapSpan urlBitmapSpan, Uri image) {
        Glide.with(activity)
            .load(image)
            .into(new BaseTarget<Drawable>() {
              @Override
              public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                urlBitmapSpan.updateBitmap(activity, resource);
              }

              @Override
              public void getSize(@NonNull SizeReadyCallback cb) {
                cb.onSizeReady(urlBitmapSpan.getPossibleSize().width(), urlBitmapSpan.getPossibleSize().height());
              }

              @Override
              public void removeCallback(@NonNull SizeReadyCallback cb) {

              }
            });

      }

    });

Enable click events

Supply an instance of a clicked observer to receive on click events on parts of the content:

        contentView.setOnSpanClickedObserver(new View.OnSpanClickedObserver() {
            @Override
            public boolean onSpanClicked(ClickableSpan span) {
                String action = span.getAction();
                action = TextUtils.isEmpty(action) ? " no action" : action;
                Toast.makeText(getContext(), action, Toast.LENGTH_LONG).show();
                return true;
            }
        });

Parse and display of HTML

 String html = "<p><i>This text is italic</i></p>";
 RichTextDocumentElement element = RichTextV2.textFromHtml(context, html);
 contentView.setText(element);

Sample Fluent Interface to create formatted text

 String paragraph = getResources().getString(R.string.sample_text);
       RichTextDocumentElement element = new RichTextDocumentElement
                .TextBuilder("What is Lorem Ipsum")
                .bold()
                .color(Color.BLUE)
                .underline(true)
                .sizeChange(1.5f)
                .center()
                .newLine()
                .image("https://netdna.webdesignerdepot.com/uploads/2013/07/icons-animation.gif",10,10)
                .click("You have clicked on the image at the top!")
                .newLine()
                .append("Click the Image above")
                .font("fonts/SourceCodePro-Bold.ttf")
                .center()
                .bold()
                .sizeChange(1.5f)
                .color(Color.RED)
                .newLine()
                .append(paragraph)
                .left()
                .image("http://random-ize.com/lorem-ipsum-generators/lorem-ipsum/lorem-ipsum.jpg")
                .click("You have clicked on the  image in the middle of the text")
                .append("Click the lorem ipsum image")
                .font("fonts/GreatVibes-Regular.otf")
                .center()
                .bold()
                .sizeChange(1.5f)
                .color(Color.RED)
                .newLine()
                .video("http://html5demos.com/assets/dizzy.mp4")
                .append("It has survived not only five centuries,")
                .color(Color.GRAY)
                .sizeChange(2.0f)
                .center()
                .append("but also the leap into electronic typesetting,")
                .strikethrough(true)
                .append("remaining essentially unchanged.")
                .click("Hello you have clicked on the text")
                .bold()
                .italic()
                .build();


    contentView.setText(element);

sample

License

RichText is licensed under the Apache v2 license:

Copyright 2017 www.square1.io

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.

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.