-
Notifications
You must be signed in to change notification settings - Fork 369
3 · Examples
The following examples should show you to setup your code.
##DocumentLayout DocumentLayout will render plain text efficiently.
###XML
android:layout_width="fill_parent"
android:layout_height="wrap_content"
document:antialias="true"
document:hypen="-"
document:lineHeightMultiplier="2.0"
document:maxLines="100"
document:offsetX="10dp"
document:offsetY="10dp"
document:paddingBottom="10dp"
document:paddingLeft="10dp"
document:paddingRight="10dp"
document:paddingTop="10dp"
document:text="@string/xml_test_data"
document:textColor="@android:color/holo_blue_bright"
document:textFormat="plain"
document:textSize="12sp"
document:textStyle="bold|strikeThrough|underline"
document:textSubpixel="true"
document:textTypefacePath="fonts/helvetica.ttf"
document:wordSpacingMultiplier="5.0" />
###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
##SpannedDocumentLayout SpannedDocumentLayout will help render Spannable text.
###XML Currently, no way to create a DocumentView with a SpannedDocumentLayout using XML.
###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
Warning: Wiki is slightly out of date at the moment
Copyright © 2012-2015 TextJustify-Android