Search in sources :

Example 1 with TextAppearanceView

use of carbon.view.TextAppearanceView in project Carbon by ZieIony.

the class Carbon method handleFontAttribute.

public static void handleFontAttribute(TextAppearanceView textView, TypedArray appearance, int textStyle, int fontWeight, int attributeId) {
    WeakReference<TextAppearanceView> textViewWeak = new WeakReference<>(textView);
    AtomicBoolean asyncFontPending = new AtomicBoolean();
    ResourcesCompat.FontCallback replyCallback = new ResourcesCompat.FontCallback() {

        @Override
        public void onFontRetrieved(@NonNull Typeface typeface) {
            if (asyncFontPending.get()) {
                TextAppearanceView textView = textViewWeak.get();
                if (textView != null)
                    textView.setTypeface(typeface, textStyle);
            }
        }

        @Override
        public void onFontRetrievalFailed(int reason) {
        }
    };
    try {
        int resourceId = appearance.getResourceId(attributeId, 0);
        TypedValue mTypedValue = new TypedValue();
        Typeface typeface = carbon.internal.ResourcesCompat.getFont(((View) textView).getContext(), resourceId, mTypedValue, textStyle, fontWeight, replyCallback);
        if (typeface != null) {
            asyncFontPending.set(true);
            textView.setTypeface(typeface, textStyle);
        }
    } catch (UnsupportedOperationException | Resources.NotFoundException ignored) {
    }
}
Also used : ResourcesCompat(androidx.core.content.res.ResourcesCompat) Typeface(android.graphics.Typeface) TextAppearanceView(carbon.view.TextAppearanceView) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WeakReference(java.lang.ref.WeakReference) NonNull(androidx.annotation.NonNull) TypedValue(android.util.TypedValue)

Example 2 with TextAppearanceView

use of carbon.view.TextAppearanceView in project Carbon by ZieIony.

the class TextMarker method onMeasure.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (id != 0) {
        TextAppearanceView textView = ((ViewGroup) getParent()).findViewById(id);
        if (text == null)
            text = textView.getText();
        textPaint = textView.getPaint();
        if (layout == null)
            layout = new StaticLayout(text, textPaint, getMeasuredWidth(), Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
        String firstLine = text.subSequence(0, layout.getLineEnd(0)).toString();
        textPaint.getTextBounds(firstLine, 0, firstLine.length(), rect);
        baseline = Math.abs(rect.top);
        rect.top = -layout.getLineAscent(0) + rect.top;
        String lastLine = text.subSequence(layout.getLineStart(layout.getLineCount() - 1), layout.getLineEnd(layout.getLineCount() - 1)).toString();
        textPaint.getTextBounds(lastLine, 0, lastLine.length(), rect2);
        rect.bottom = layout.getHeight() - layout.getLineDescent(layout.getLineCount() - 1) + rect2.bottom;
        setMeasuredDimension(getMeasuredWidth(), rect.height() + getPaddingTop() + getPaddingBottom());
    }
}
Also used : ViewGroup(android.view.ViewGroup) StaticLayout(android.text.StaticLayout) TextAppearanceView(carbon.view.TextAppearanceView)

Aggregations

TextAppearanceView (carbon.view.TextAppearanceView)2 Typeface (android.graphics.Typeface)1 StaticLayout (android.text.StaticLayout)1 TypedValue (android.util.TypedValue)1 ViewGroup (android.view.ViewGroup)1 NonNull (androidx.annotation.NonNull)1 ResourcesCompat (androidx.core.content.res.ResourcesCompat)1 WeakReference (java.lang.ref.WeakReference)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1