TextLayoutBuilder layoutBuilder = new TextLayoutBuilder(); layoutBuilder.setShouldCacheLayout(false); .setDensity(density) .setEllipsize(ellipsize) .setMaxLines(maxLines) .setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor) .setSingleLine(isSingleLine) .setText(text) .setTextSize(textSize) .setWidth(SizeSpec.getSize(widthSpec), textMeasureMode) .setIncludeFontPadding(shouldIncludeFontPadding) .setTextSpacingExtra(extraSpacing) .setTextSpacingMultiplier(spacingMultiplier) .setAlignment(textAlignment) .setLinkColor(linkColor) .setJustificationMode(justificationMode) .setBreakStrategy(breakStrategy) .setHyphenationFrequency(hyphenationFrequency); layoutBuilder.setLineHeight(lineHeight); layoutBuilder.setLetterSpacing(letterSpacing); layoutBuilder.setMinEms(minEms); } else { layoutBuilder.setMinWidth(minTextWidth); layoutBuilder.setMaxEms(maxEms);
/** * Returns the line height for this TextLayoutBuilder. * * @return The line height used by this TextLayoutBuilder */ public float getLineHeight() { return mParams.getLineHeight(); }
/** * Sets the density of this layout. This should typically be set to your current display's density * * @param density The density desired * @return This {@link TextLayoutBuilder} */ public TextLayoutBuilder setDensity(float density) { if (mParams.paint.density != density) { mParams.createNewPaintIfNeeded(); mParams.paint.density = density; mSavedLayout = null; } return this; }
@Test public void testDoNotWarmText() { FakeGlyphWarmer warmer = new FakeGlyphWarmer(); mLayout = mBuilder.setShouldWarmText(false).setGlyphWarmer(warmer).build(); assertEquals(warmer.getLayout(), null); }
@Test public void testCaching() { mLayout = mBuilder.setShouldCacheLayout(true).build(); Layout newLayout = mBuilder.build(); assertEquals(mLayout, newLayout); assertEquals(mBuilder.sCache.size(), 1); assertEquals(mBuilder.sCache.get(mBuilder.mParams.hashCode()), mLayout); }
@Test public void testSetTypeface() { mLayout = mBuilder.setTypeface(Typeface.MONOSPACE).build(); assertEquals(mBuilder.getTypeface(), Typeface.MONOSPACE); }
@Test public void testSetGlyphWarmer() { FakeGlyphWarmer glyphWarmer = new FakeGlyphWarmer(); mLayout = mBuilder.setGlyphWarmer(glyphWarmer).build(); assertEquals(mBuilder.getGlyphWarmer(), glyphWarmer); }
@Test public void testSetTextDirection() { mLayout = mBuilder.setTextDirection(TextDirectionHeuristicsCompat.LOCALE).build(); assertEquals(mBuilder.getTextDirection(), TextDirectionHeuristicsCompat.LOCALE); }
@Test public void testSetShouldWarmText() { mLayout = mBuilder.setShouldWarmText(true).build(); assertEquals(mBuilder.getShouldWarmText(), true); }
@Before public void setup() { mBuilder = new TextLayoutBuilder(); mBuilder.setText(TEST); // Clear the cache. TextLayoutBuilder.sCache.evictAll(); }
/** * Sets a text style for the layout. * * @param style The text style for the layout * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setTextStyle(int style) { return setTypeface(Typeface.defaultFromStyle(style)); }
/** * Sets the intended width of the text layout. * * @param width The width of the text layout * @return This {@link TextLayoutBuilder} instance * @see #setWidth(int, int) */ public TextLayoutBuilder setWidth(@Px int width) { return setWidth(width, width <= 0 ? MEASURE_MODE_UNSPECIFIED : MEASURE_MODE_EXACTLY); }
/** * Sets text letter-spacing in em units. Typical values for slight expansion will be around 0.05. * Negative values tighten text. * * @param letterSpacing A text letter-space value in ems. * @see #getLetterSpacing() */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TextLayoutBuilder setLetterSpacing(float letterSpacing) { if (getLetterSpacing() != letterSpacing) { mParams.createNewPaintIfNeeded(); mParams.paint.setLetterSpacing(letterSpacing); mSavedLayout = null; } return this; }
/** * Sets the values for a TextLayoutBuilder from a style resource. * * @param builder The TextLayoutBuilder * @param context The Context to use for resolving the attributes * @param styleRes The style resource identifier */ public static void updateFromStyleResource( TextLayoutBuilder builder, Context context, @StyleRes int styleRes) { updateFromStyleResource(builder, context, 0, styleRes); }
@Test public void testWarmText() { FakeGlyphWarmer warmer = new FakeGlyphWarmer(); mLayout = mBuilder.setShouldWarmText(true).setGlyphWarmer(warmer).build(); assertEquals(warmer.getLayout(), mLayout); }
@Test public void testNoCaching() { mLayout = mBuilder.setShouldCacheLayout(false).build(); Layout newLayout = mBuilder.build(); assertNotEquals(mLayout, newLayout); assertEquals(mBuilder.sCache.size(), 0); assertEquals(mBuilder.sCache.get(mBuilder.mParams.hashCode()), null); }
/** * Sets the link color for the text in the layout. * * @param linkColor The link color * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setLinkColor(@ColorInt int linkColor) { if (mParams.paint.linkColor != linkColor) { mParams.createNewPaintIfNeeded(); mParams.paint.linkColor = linkColor; mSavedLayout = null; } return this; }
/** * Sets the text size for the layout. * * @param size The text size in pixels * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setTextSize(int size) { if (mParams.paint.getTextSize() != size) { mParams.createNewPaintIfNeeded(); mParams.paint.setTextSize(size); mSavedLayout = null; } return this; }
/** * Sets the typeface used by this TextLayoutBuilder. * * @param typeface The typeface for this TextLayoutBuilder * @return This {@link TextLayoutBuilder} instance */ public TextLayoutBuilder setTypeface(Typeface typeface) { if (mParams.paint.getTypeface() != typeface) { mParams.createNewPaintIfNeeded(); mParams.paint.setTypeface(typeface); mSavedLayout = null; } return this; }