Skip to content

Latest commit

 

History

History

README.md

THE NEW TEXT JUSTIFY FOR ANDROID - SUPPORT FOR SPANNABLES!

NOTE: THIS IS A WORK IN PROGRESS. PORTIONS OF THIS README ARE INCOMPLETE

JUST COMPILE THE EXAMPLE TO SEE HOW IT LOOKS

Logo

Android Full Justification

#About This library will provide you a way to justify text. It supports both plain text and Spannables. Additionally, the library can auto-hyphentate your displayed content.

#Donate If for some reason you like the library and feel like thanking me. Here you go! Thank you in advance.

Donate

#Known Issues

Status Issues
OPEN Scroll caching for very large documents i.e. > 4000 paragaphs
OPEN Support RTL languages; reverse text
OPEN Support more features like TextView in terms of Paint settings

#Preview Screenshot from the example in com.text.examples.SimpleExample.

Preview

##Chinese Characters

Chinese

##Custom Spans

CustomSpans CustomSpans

#Examples

##Basic Usage - Plain Text Creating a DocumentView and enabling justification.

// Create DocumentView and set plain text
// Important: Use DocumentLayout.class
DocumentView documentView = new DocumentView(this, DocumentLayout.class);  // Support plain text
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentView.setText("Insert your text here", true); // Set to `true` to enable justification

##Basic Usage - Spanned Creating a DocumentView and setting some features such as padding and typeface

/*
 * To mark a region for justification in Spanned strings, you must setSpan(new JustifySpan(), start, end)
 * that particular region.
 */

// Create span
Spannable span = new SpannableString("In New York and New Jersey, governors Andrew Cuomo and Chris Christie have implemented controversial quarantines.");

// Set region to justify
span.setSpan(new JustifySpan(), 0, span.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

// Create DocumentView and set span.
// Important: Use SpannedDocumentLayout.class
DocumentView documentView = new DocumentView(this, SpannedDocumentLayout.class);  // Support spanned text

// Set the fallback alignment if an alignment is not specified for a line
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED); 

documentView.setText(span, true); // Set to `true` to enable justification

##Advanced Usage - Spanned

/*
 * Class that builds spanned documents and enables multiple Spans to be set at once
 * to the last inserted text
 */

// Helper class; NOT required for DocumentView
class ArticleBuilder extends SpannableStringBuilder {
    public ArticleBuilder append(CharSequence text, boolean newline, Object ... spans){
        int start = this.length();
        this.append(Html.fromHtml("<p>" + text + "</p>" + (newline ? "<br>" : "")));
        for(Object span : spans) {
            this.setSpan(span, start, this.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        return this;
    }
}

// NOTE: ArticleBuilder is NOT required for DocumentView to work.
//       It is just a helper class I made to make it easier to add
//       multiple spans to a certain text.
ArticleBuilder a = new ArticleBuilder();
a.append("WHO: Ebola Cases",
          false, new RelativeSizeSpan(2f), new StyleSpan(Typeface.BOLD))
 .append("<font color=0xFFC801>Sam Frizell</font><font color=0x888888> @Sam_Frizell  Oct. 25, 2014</font>",
          false, new RelativeSizeSpan(0.8f), new StyleSpan(Typeface.BOLD))
 .append("In New York and New Jersey, governors Andrew Cuomo and Chris Christie have implemented controversial quarantines on all healthcare workers returning from West Africa after a doctor returning from Guinea contracted the disease and was diagnosed in New York.",
          true, new RelativeSizeSpan(1f), new JustifySpan());

DocumentView documentView = new DocumentView(this, SpannedDocumentLayout.class);  // Support spanned text
documentView.setText(a, true); // Set to `true` to enable justification

##More Refer to com.text.examples.SimpleExample for an Android application that demonstrates some of the features.

#API

##DocumentView extends View ###Class Overview Displays static text to user. Similar to TextView but provides additional features to support text-justification and auto-hyphenation.

####Constructors

Description
DocumentView(Context context)
DocumentView(Context context, AttributeSet attrs)
DocumentView(Context context, AttributeSet attrs, int defStyle)
DocumentView(Context context, Class<? extends DocumentLayout> layoutClass)
Constructs a DocumentView with the speficied DocumentLayout

####Public Methods

Return Description
void setTextSize(float textSize)
Set the internal Paint text size.
void setColor(int textColor)
Set the internal Paint foreground color.
void setTypeface(Typeface typeface)
Sets the typeface and style in which the text should be displayed.
void setText(CharSequence source)
Set the content that needs to be displayed.
CharSequence getText()
Return the text the this is displaying.
DocumentLayout.LayoutParams getDocumentLayoutParams()
Get the LayoutParams associated with this view.
DocumentLayout getLayout()
Get the DocumentLayout associated with this view.

##DocumentLayout ###Class Overview Calculates the position of each line/word/paragraph. Call measure to calculate the positions. Call draw to render the text.

####Constructors

Description
DocumentLayout(Paint paint)
Constructs a DocumentLayout with the specified the Paint which will be used to draw

####Public Methods

Return Description
void setText(CharSequence source)
Set the content that needs to be displayed.
void getLayoutParams()
Get the LayoutParams that would be used to configure the layout settings.
CharSequence getText()
Return the text the this is displaying.
int getMeasuredHeight()
Get the height calculated by the layout with the specified text and Paint.
void measure()
Construct the static text positions.
void draw(Canvas canvas)
Draw the content measured by the layout onto the specified Canvas.

##DocumentLayout ###Class Overview Calculates the position of each line/word/paragraph. Call measure to calculate the positions. Call draw to render the text.

####Constructors

Description
DocumentLayout(Paint paint)
Constructs a DocumentLayout with the specified the Paint which will be used to draw

####Public Methods

Return Description
void setText(CharSequence source)
Set the content that needs to be displayed.
void getLayoutParams()
Get the LayoutParams that would be used to configure the layout settings.
CharSequence getText()
Return the text the this is displaying.
int getMeasuredHeight()
Get the height calculated by the layout with the specified text and Paint.
void measure()
Construct the static text positions.
void draw(Canvas canvas)
Draw the content measured by the layout onto the specified Canvas.

##Document.TextAlignment ###Class Overview Specify the alignment for text inside the DocumentLayout by using these constants.

####Constructors

Description
LEFT
Align the text to the left of the view
CENTER
Align the text at the center of the view
RIGHT
Align the text to the right of the view
JUSTIFIED
Wraps the text the view

##DocumentLayout.LayoutParams ###Enum Overview Calculates the position of each line/word/paragraph. Call measure to calculate the positions. Call draw to render the text.

####Constructors

Description
DocumentLayout(Paint paint)
Constructs a DocumentLayout with the specified the Paint which will be used to draw

####Public Methods

Return Description
void setTextAlignment(TextAlignment textAlignment)
Set the content that needs to be displayed.
void setHyphenator(Hyphenator hyphenator)
Set the content that needs to be displayed.
void setLeft(float left)
Set the content that needs to be displayed.
void setTop(float left)
Set the content that needs to be displayed.
void setBottom(float left)
Set the content that needs to be displayed.
void setRight(float left)
Set the content that needs to be displayed.
void setParentWidth(float parentWidth)
Set the content that needs to be displayed.
void setOffsetX(float offsetX)
Set the content that needs to be displayed.
void setOffsetY(float offsetY)
Set the content that needs to be displayed.
void setLineHeightMultiplier(float lineHeightMultiplier)
Set the content that needs to be displayed.
void setHyphenated(boolean hyphenate)
Set the content that needs to be displayed.
void setMaxLines(int maxLines)
Set the content that needs to be displayed.
void setHyphen(String hyphen)
Set the content that needs to be displayed.
void setReverse(boolean reverse)
Set the content that needs to be displayed.
void getTextAlignment(TextAlignment textAlignment)
Set the content that needs to be displayed.
void getHyphenator(Hyphenator hyphenator)
Set the content that needs to be displayed.
void getLeft(float left)
Set the content that needs to be displayed.
void getTop(float left)
Set the content that needs to be displayed.
void getBottom(float left)
Set the content that needs to be displayed.
void getRight(float left)
Set the content that needs to be displayed.
void getParentWidth(float parentWidth)
Set the content that needs to be displayed.
void getOffsetX(float offsetX)
Set the content that needs to be displayed.
void getOffsetY(float offsetY)
Set the content that needs to be displayed.
void getLineHeightMultiplier(float lineHeightMultiplier)
Set the content that needs to be displayed.
void isHyphenated(boolean hyphenate)
Set the content that needs to be displayed.
void getMaxLines(int maxLines)
Set the content that needs to be displayed.
void getHyphen(String hyphen)
Set the content that needs to be displayed.
void isReverse(boolean reverse)
Set the content that needs to be displayed.