Skip to content
This repository has been archived by the owner on Apr 27, 2019. It is now read-only.

3 · Examples

bluejamesbond edited this page Jan 4, 2015 · 16 revisions

The following examples should show you to setup your code.

##DocumentLayout DocumentLayout will render plain text efficiently.

###XML

<com.bluejamesbond.text.DocumentView xmlns:document="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    document:antialias="true"
    document:lineHeightMultiplier="2.0"
    document:maxLines="100"
    document:offsetX="10dp"
    document:offsetY="10dp"
    document:padding="10dp"
    document:paddingBottom="10dp"
    document:paddingLeft="10dp"
    document:paddingRight="10dp"
    document:paddingTop="10dp"
    document:text="@string/xml_test_data"
    document:textAlignment="justified"
    document:textColor="@android:color/white"
    document:textFormat="plain"
    document:textSize="12sp"
    document:textStyle="bold|strikeThrough|underline"
    document:textSubpixel="true"
    document:textTypefacePath="fonts/helvetica.ttf"
    document:wordSpacingMultiplier="5.0"
    document:cacheConfig="auto_quality" />

###Java The following example will create a DocumentView and enable justification.

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

###Hyphenator Please refer to HyphenatorTest.java.

##SpannedDocumentLayout

###XML

<com.bluejamesbond.text.DocumentView xmlns:document="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    document:antialias="true"
    document:lineHeightMultiplier="2.0"
    document:maxLines="100"
    document:offsetX="10dp"
    document:offsetY="10dp"
    document:padding="10dp"
    document:paddingBottom="10dp"
    document:paddingLeft="10dp"
    document:paddingRight="10dp"
    document:paddingTop="10dp"
    document:reverse="false"
    document:text="@string/xml_test_data"
    document:textAlignment="justified"
    document:textColor="@android:color/white"
    document:textFormat="formatted"
    document:textSize="12sp"
    document:textStyle="bold|strikeThrough|underline"
    document:textSubpixel="true"
    document:textTypefacePath="fonts/helvetica.ttf"
    document:cacheConfig="auto_quality" />

###Java The following example will create a DocumentView and set 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, DocumentView.FORMATTED_TEXT);  // 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

##ArticleBuilder ArticleBuilder is a class that builds spanned documents and enables multiple Spans to be set at once to the last inserted text. In order to use ArticleBuilder, you must import com.text.demo.helper#ArticleBuilder.

// 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
Clone this wiki locally