Example usage for android.text TextPaint getFontMetricsInt

List of usage examples for android.text TextPaint getFontMetricsInt

Introduction

In this page you can find the example usage for android.text TextPaint getFontMetricsInt.

Prototype

public int getFontMetricsInt(FontMetricsInt fmi) 

Source Link

Document

Return the font's interline spacing, given the Paint's settings for typeface, textSize, etc.

Usage

From source file:com.facebook.litho.widget.TextSpec.java

@OnMeasure
static void onMeasure(ComponentContext context, ComponentLayout layout, int widthSpec, int heightSpec,
        Size size, @Prop(resType = ResType.STRING) CharSequence text,
        @Prop(optional = true) TruncateAt ellipsize,
        @Prop(optional = true, resType = ResType.BOOL) boolean shouldIncludeFontPadding,
        @Prop(optional = true, resType = ResType.INT) int minLines,
        @Prop(optional = true, resType = ResType.INT) int maxLines,
        @Prop(optional = true, resType = ResType.INT) int minEms,
        @Prop(optional = true, resType = ResType.INT) int maxEms,
        @Prop(optional = true, resType = ResType.DIMEN_SIZE) int minWidth,
        @Prop(optional = true, resType = ResType.DIMEN_SIZE) int maxWidth,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowRadius,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDx,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDy,
        @Prop(optional = true, resType = ResType.COLOR) int shadowColor,
        @Prop(optional = true, resType = ResType.BOOL) boolean isSingleLine,
        @Prop(optional = true, resType = ResType.COLOR) int textColor,
        @Prop(optional = true) ColorStateList textColorStateList,
        @Prop(optional = true, resType = ResType.COLOR) int linkColor,
        @Prop(optional = true, resType = ResType.DIMEN_TEXT) int textSize,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float extraSpacing,
        @Prop(optional = true, resType = ResType.FLOAT) float spacingMultiplier,
        @Prop(optional = true) int textStyle, @Prop(optional = true) Typeface typeface,
        @Prop(optional = true) Alignment textAlignment, @Prop(optional = true) boolean glyphWarming,
        @Prop(optional = true) TextDirectionHeuristicCompat textDirection, Output<Layout> measureLayout,
        Output<Integer> measuredWidth, Output<Integer> measuredHeight) {

    if (TextUtils.isEmpty(text)) {
        measureLayout.set(null);//from  w w w. ja v  a2  s  .  c  o m
        size.width = 0;
        size.height = 0;
        return;
    }

    Layout newLayout = createTextLayout(widthSpec, ellipsize, shouldIncludeFontPadding, maxLines, shadowRadius,
            shadowDx, shadowDy, shadowColor, isSingleLine, text, textColor, textColorStateList, linkColor,
            textSize, extraSpacing, spacingMultiplier, textStyle, typeface, textAlignment, glyphWarming,
            layout.getResolvedLayoutDirection(), minEms, maxEms, minWidth, maxWidth, textDirection);

    measureLayout.set(newLayout);

    size.width = SizeSpec.resolveSize(widthSpec, newLayout.getWidth());

    // Adjust height according to the minimum number of lines.
    int preferredHeight = LayoutMeasureUtil.getHeight(newLayout);
    final int lineCount = newLayout.getLineCount();
    if (lineCount < minLines) {
        final TextPaint paint = newLayout.getPaint();

        final int lineHeight = Math.round(paint.getFontMetricsInt(null) * spacingMultiplier + extraSpacing);
        preferredHeight += lineHeight * (minLines - lineCount);
    }

    size.height = SizeSpec.resolveSize(heightSpec, preferredHeight);

    // Some devices seem to be returning negative sizes in some cases.
    if (size.width < 0 || size.height < 0) {
        size.width = Math.max(size.width, 0);
        size.height = Math.max(size.height, 0);

        final ComponentsLogger logger = context.getLogger();
        if (logger != null) {
            final LogEvent event = logger.newEvent(EVENT_ERROR);
            event.addParam(PARAM_MESSAGE, "Text layout measured to less than 0 pixels");
            logger.log(event);
        }
    }

    measuredWidth.set(size.width);
    measuredHeight.set(size.height);
}