Search in sources :

Example 6 with ScaleXSpan

use of android.text.style.ScaleXSpan in project mongol-library by suragch.

the class MongolTextViewActivity method onSpanClick.

public void onSpanClick(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Add span");
    final String[] spanTypes = { "BackgroundColorSpan", "ForegroundColorSpan", "RelativeSizeSpan", "TypefaceSpan", "ScaleXSpan", "StyleSpan", "SubscriptSpan", "SuperscriptSpan", "UnderlineSpan", "ClickableSpan" };
    final SpannableString spannableString = new SpannableString(mtvExample.getText().toString());
    // check boxes
    builder.setMultiChoiceItems(spanTypes, null, new DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            // user checked or unchecked a box
            // select the middle third
            int start = mtvExample.getText().length() / 3;
            int end = mtvExample.getText().length() * 2 / 3;
            switch(which) {
                case // highlight
                0:
                    if (isChecked) {
                        BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        BackgroundColorSpan[] spans = spannableString.getSpans(0, spannableString.length(), BackgroundColorSpan.class);
                        for (BackgroundColorSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // text color
                1:
                    if (isChecked) {
                        ForegroundColorSpan span = new ForegroundColorSpan(Color.GREEN);
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        ForegroundColorSpan[] spans = spannableString.getSpans(0, spannableString.length(), ForegroundColorSpan.class);
                        for (ForegroundColorSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // SIZE
                2:
                    if (isChecked) {
                        RelativeSizeSpan span = new RelativeSizeSpan(2f);
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        RelativeSizeSpan[] spans = spannableString.getSpans(0, spannableString.length(), RelativeSizeSpan.class);
                        for (RelativeSizeSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // FONT
                3:
                    // setting a custom font as a span requires the custom MongolTypefaceSpan
                    if (isChecked) {
                        Typeface font = MongolFont.get(MongolFontActivity.JCLGQ, getApplicationContext());
                        MongolTypefaceSpan span = new MongolTypefaceSpan(font);
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        MongolTypefaceSpan[] spans = spannableString.getSpans(0, spannableString.length(), MongolTypefaceSpan.class);
                        for (MongolTypefaceSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // scale X
                4:
                    if (isChecked) {
                        ScaleXSpan span = new ScaleXSpan(2f);
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        ScaleXSpan[] spans = spannableString.getSpans(0, spannableString.length(), ScaleXSpan.class);
                        for (ScaleXSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // bold style
                5:
                    if (isChecked) {
                        // TODO is there a correct way to apply ITALIC to Mongolian text (can adjust paint.setTextSkewX)
                        // StyleSpan span = new StyleSpan(Typeface.ITALIC);
                        StyleSpan span = new StyleSpan(Typeface.BOLD);
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        StyleSpan[] spans = spannableString.getSpans(0, spannableString.length(), StyleSpan.class);
                        for (StyleSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // Subscript
                6:
                    if (isChecked) {
                        SubscriptSpan span = new SubscriptSpan();
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        SubscriptSpan[] spans = spannableString.getSpans(0, spannableString.length(), SubscriptSpan.class);
                        for (SubscriptSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // Superscript
                7:
                    if (isChecked) {
                        SuperscriptSpan span = new SuperscriptSpan();
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        SuperscriptSpan[] spans = spannableString.getSpans(0, spannableString.length(), SuperscriptSpan.class);
                        for (SuperscriptSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // Underline
                8:
                    if (isChecked) {
                        UnderlineSpan span = new UnderlineSpan();
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        UnderlineSpan[] spans = spannableString.getSpans(0, spannableString.length(), UnderlineSpan.class);
                        for (UnderlineSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
                    break;
                case // Clickable
                9:
                    if (isChecked) {
                        ClickableSpan span = new ClickableSpan() {

                            @Override
                            public void onClick(View view) {
                                Toast.makeText(MongolTextViewActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
                            }
                        };
                        spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        mtvExample.setMovementMethod(LinkMovementMethod.getInstance());
                    } else {
                        ClickableSpan[] spans = spannableString.getSpans(0, spannableString.length(), ClickableSpan.class);
                        for (ClickableSpan span : spans) {
                            spannableString.removeSpan(span);
                        }
                    }
            }
        }
    });
    // dialog buttons
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mtvExample.setText(spannableString);
        }
    });
    builder.setNegativeButton("Cancel", null);
    AlertDialog dialog = builder.create();
    dialog.show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) DialogInterface(android.content.DialogInterface) Typeface(android.graphics.Typeface) ScaleXSpan(android.text.style.ScaleXSpan) SpannableString(android.text.SpannableString) RelativeSizeSpan(android.text.style.RelativeSizeSpan) ClickableSpan(android.text.style.ClickableSpan) MongolTextView(net.studymongolian.mongollibrary.MongolTextView) View(android.view.View) AdapterView(android.widget.AdapterView) TextPaint(android.text.TextPaint) UnderlineSpan(android.text.style.UnderlineSpan) SpannableString(android.text.SpannableString) MongolTypefaceSpan(net.studymongolian.mongollibrary.MongolTypefaceSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) BackgroundColorSpan(android.text.style.BackgroundColorSpan)

Example 7 with ScaleXSpan

use of android.text.style.ScaleXSpan in project bitcoin-wallet by bitcoin-wallet.

the class CurrencyTextView method setPrefixScaleX.

public void setPrefixScaleX(final float prefixScaleX) {
    this.prefixScaleXSpan = new ScaleXSpan(prefixScaleX);
    updateView();
}
Also used : ScaleXSpan(android.text.style.ScaleXSpan)

Example 8 with ScaleXSpan

use of android.text.style.ScaleXSpan in project EnableHands by LeviWGG.

the class SpanUtils method updateCharCharSequence.

private void updateCharCharSequence() {
    if (mText.length() == 0)
        return;
    int start = mBuilder.length();
    mBuilder.append(mText);
    int end = mBuilder.length();
    if (foregroundColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new ForegroundColorSpan(foregroundColor), start, end, flag);
    }
    if (backgroundColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new BackgroundColorSpan(backgroundColor), start, end, flag);
    }
    if (first != -1) {
        mBuilder.setSpan(new LeadingMarginSpan.Standard(first, rest), start, end, flag);
    }
    if (quoteColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new CustomQuoteSpan(quoteColor, stripeWidth, quoteGapWidth), start, end, flag);
    }
    if (bulletColor != COLOR_DEFAULT) {
        mBuilder.setSpan(new CustomBulletSpan(bulletColor, bulletRadius, bulletGapWidth), start, end, flag);
    }
    // }
    if (fontSize != -1) {
        mBuilder.setSpan(new AbsoluteSizeSpan(fontSize, fontSizeIsDp), start, end, flag);
    }
    if (proportion != -1) {
        mBuilder.setSpan(new RelativeSizeSpan(proportion), start, end, flag);
    }
    if (xProportion != -1) {
        mBuilder.setSpan(new ScaleXSpan(xProportion), start, end, flag);
    }
    if (lineHeight != -1) {
        mBuilder.setSpan(new CustomLineHeightSpan(lineHeight, alignLine), start, end, flag);
    }
    if (isStrikethrough) {
        mBuilder.setSpan(new StrikethroughSpan(), start, end, flag);
    }
    if (isUnderline) {
        mBuilder.setSpan(new UnderlineSpan(), start, end, flag);
    }
    if (isSuperscript) {
        mBuilder.setSpan(new SuperscriptSpan(), start, end, flag);
    }
    if (isSubscript) {
        mBuilder.setSpan(new SubscriptSpan(), start, end, flag);
    }
    if (isBold) {
        mBuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, flag);
    }
    if (isItalic) {
        mBuilder.setSpan(new StyleSpan(Typeface.ITALIC), start, end, flag);
    }
    if (isBoldItalic) {
        mBuilder.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, end, flag);
    }
    if (fontFamily != null) {
        mBuilder.setSpan(new TypefaceSpan(fontFamily), start, end, flag);
    }
    if (typeface != null) {
        mBuilder.setSpan(new CustomTypefaceSpan(typeface), start, end, flag);
    }
    if (alignment != null) {
        mBuilder.setSpan(new AlignmentSpan.Standard(alignment), start, end, flag);
    }
    if (clickSpan != null) {
        mBuilder.setSpan(clickSpan, start, end, flag);
    }
    if (url != null) {
        mBuilder.setSpan(new URLSpan(url), start, end, flag);
    }
    if (blurRadius != -1) {
        mBuilder.setSpan(new MaskFilterSpan(new BlurMaskFilter(blurRadius, style)), start, end, flag);
    }
    if (shader != null) {
        mBuilder.setSpan(new ShaderSpan(shader), start, end, flag);
    }
    if (shadowRadius != -1) {
        mBuilder.setSpan(new ShadowSpan(shadowRadius, shadowDx, shadowDy, shadowColor), start, end, flag);
    }
    if (spans != null) {
        for (Object span : spans) {
            mBuilder.setSpan(span, start, end, flag);
        }
    }
}
Also used : AlignmentSpan(android.text.style.AlignmentSpan) SuperscriptSpan(android.text.style.SuperscriptSpan) ScaleXSpan(android.text.style.ScaleXSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) SubscriptSpan(android.text.style.SubscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) MaskFilterSpan(android.text.style.MaskFilterSpan) StyleSpan(android.text.style.StyleSpan) LeadingMarginSpan(android.text.style.LeadingMarginSpan) BlurMaskFilter(android.graphics.BlurMaskFilter) BackgroundColorSpan(android.text.style.BackgroundColorSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) TypefaceSpan(android.text.style.TypefaceSpan)

Example 9 with ScaleXSpan

use of android.text.style.ScaleXSpan in project FirebaseUI-Android by firebase.

the class SpacedEditText method getSpacedOutString.

private SpannableStringBuilder getSpacedOutString(CharSequence text) {
    SpannableStringBuilder builder = new SpannableStringBuilder();
    int textLength = text.length();
    int lastSpaceIndex = -1;
    // Scale the space without scaling the character to preserve font appearance
    for (int i = 0; i < textLength - 1; i++) {
        builder.append(text.charAt(i));
        builder.append(" ");
        lastSpaceIndex += 2;
        builder.setSpan(new ScaleXSpan(mProportion), lastSpaceIndex, lastSpaceIndex + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    // Append the last character
    if (textLength != 0)
        builder.append(text.charAt(textLength - 1));
    return builder;
}
Also used : ScaleXSpan(android.text.style.ScaleXSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 10 with ScaleXSpan

use of android.text.style.ScaleXSpan in project Tehreer-Android by mta452.

the class ShapingRunLocator method resolveSpans.

private static void resolveSpans(@NonNull ShapingRun shapingRun, @NonNull Object[] spans) {
    for (Object span : spans) {
        if (span instanceof com.mta.tehreer.layout.style.TypefaceSpan) {
            com.mta.tehreer.layout.style.TypefaceSpan typefaceSpan = (com.mta.tehreer.layout.style.TypefaceSpan) span;
            shapingRun.typeface = typefaceSpan.getTypeface();
            shapingRun.typeWeight = shapingRun.typeface.getWeight();
            shapingRun.typeSlope = shapingRun.typeface.getSlope();
        } else if (span instanceof TypeSizeSpan) {
            TypeSizeSpan typeSizeSpan = (TypeSizeSpan) span;
            shapingRun.typeSize = typeSizeSpan.getSize();
        } else if (span instanceof TypefaceSpan) {
            TypefaceSpan typefaceSpan = (TypefaceSpan) span;
            resolveTypeface(shapingRun, typefaceSpan.getFamily(), TypeWidth.NORMAL);
        } else if (span instanceof AbsoluteSizeSpan) {
            AbsoluteSizeSpan absoluteSizeSpan = (AbsoluteSizeSpan) span;
            shapingRun.typeSize = absoluteSizeSpan.getSize();
        } else if (span instanceof RelativeSizeSpan) {
            RelativeSizeSpan relativeSizeSpan = (RelativeSizeSpan) span;
            shapingRun.typeSize *= relativeSizeSpan.getSizeChange();
        } else if (span instanceof StyleSpan) {
            StyleSpan styleSpan = (StyleSpan) span;
            resolveStyle(shapingRun, styleSpan.getStyle());
            updateTypeface(shapingRun);
        } else if (span instanceof TextAppearanceSpan) {
            TextAppearanceSpan appearanceSpan = (TextAppearanceSpan) span;
            shapingRun.typeSize = appearanceSpan.getTextSize();
            resolveStyle(shapingRun, appearanceSpan.getTextStyle());
            String familyName = appearanceSpan.getFamily();
            if (familyName != null) {
                resolveTypeface(shapingRun, familyName, TypeWidth.NORMAL);
            } else {
                updateTypeface(shapingRun);
            }
        } else if (span instanceof ScaleXSpan) {
            ScaleXSpan scaleXSpan = (ScaleXSpan) span;
            shapingRun.scaleX = scaleXSpan.getScaleX();
        } else if (span instanceof SuperscriptSpan) {
            resolveBaselineShift(shapingRun, 0.5f);
        } else if (span instanceof SubscriptSpan) {
            resolveBaselineShift(shapingRun, -0.5f);
        } else if (span instanceof ReplacementSpan) {
            shapingRun.replacement = (ReplacementSpan) span;
        }
    }
    if (shapingRun.typeSize < 0.0f) {
        shapingRun.typeSize = 0.0f;
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) SuperscriptSpan(android.text.style.SuperscriptSpan) ScaleXSpan(android.text.style.ScaleXSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) ReplacementSpan(android.text.style.ReplacementSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) TypeSizeSpan(com.mta.tehreer.layout.style.TypeSizeSpan) SubscriptSpan(android.text.style.SubscriptSpan) TypefaceSpan(android.text.style.TypefaceSpan)

Aggregations

ScaleXSpan (android.text.style.ScaleXSpan)10 RelativeSizeSpan (android.text.style.RelativeSizeSpan)6 StyleSpan (android.text.style.StyleSpan)6 SubscriptSpan (android.text.style.SubscriptSpan)6 SuperscriptSpan (android.text.style.SuperscriptSpan)6 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)5 BackgroundColorSpan (android.text.style.BackgroundColorSpan)5 ForegroundColorSpan (android.text.style.ForegroundColorSpan)5 TypefaceSpan (android.text.style.TypefaceSpan)5 UnderlineSpan (android.text.style.UnderlineSpan)5 SuppressLint (android.annotation.SuppressLint)4 BlurMaskFilter (android.graphics.BlurMaskFilter)4 TextPaint (android.text.TextPaint)4 MaskFilterSpan (android.text.style.MaskFilterSpan)4 StrikethroughSpan (android.text.style.StrikethroughSpan)4 URLSpan (android.text.style.URLSpan)4 Paint (android.graphics.Paint)3 SpannableStringBuilder (android.text.SpannableStringBuilder)3 AlignmentSpan (android.text.style.AlignmentSpan)3 LeadingMarginSpan (android.text.style.LeadingMarginSpan)3