Search in sources :

Example 61 with StyleSpan

use of android.text.style.StyleSpan in project WordPress-Android by wordpress-mobile.

the class NotificationsUtils method getSpannableContentForRanges.

/**
     * Returns a spannable with formatted content based on WP.com note content 'range' data
     * @param blockObject the JSON data
     * @param textView the TextView that will display the spannnable
     * @param onNoteBlockTextClickListener - click listener for ClickableSpans in the spannable
     * @param isFooter - Set if spannable should apply special formatting
     * @return Spannable string with formatted content
     */
public static Spannable getSpannableContentForRanges(JSONObject blockObject, TextView textView, final NoteBlock.OnNoteBlockTextClickListener onNoteBlockTextClickListener, boolean isFooter) {
    if (blockObject == null) {
        return new SpannableStringBuilder();
    }
    String text = blockObject.optString("text", "");
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
    boolean shouldLink = onNoteBlockTextClickListener != null;
    // Add ImageSpans for note media
    addImageSpansForBlockMedia(textView, blockObject, spannableStringBuilder);
    // Process Ranges to add links and text formatting
    JSONArray rangesArray = blockObject.optJSONArray("ranges");
    if (rangesArray != null) {
        for (int i = 0; i < rangesArray.length(); i++) {
            JSONObject rangeObject = rangesArray.optJSONObject(i);
            if (rangeObject == null) {
                continue;
            }
            NoteBlockClickableSpan clickableSpan = new NoteBlockClickableSpan(WordPress.getContext(), rangeObject, shouldLink, isFooter) {

                @Override
                public void onClick(View widget) {
                    if (onNoteBlockTextClickListener != null) {
                        onNoteBlockTextClickListener.onNoteBlockTextClicked(this);
                    }
                }
            };
            int[] indices = clickableSpan.getIndices();
            if (indices.length == 2 && indices[0] <= spannableStringBuilder.length() && indices[1] <= spannableStringBuilder.length()) {
                spannableStringBuilder.setSpan(clickableSpan, indices[0], indices[1], Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                // Add additional styling if the range wants it
                if (clickableSpan.getSpanStyle() != Typeface.NORMAL) {
                    StyleSpan styleSpan = new StyleSpan(clickableSpan.getSpanStyle());
                    spannableStringBuilder.setSpan(styleSpan, indices[0], indices[1], Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                }
            }
        }
    }
    return spannableStringBuilder;
}
Also used : NoteBlockClickableSpan(org.wordpress.android.ui.notifications.blocks.NoteBlockClickableSpan) JSONObject(org.json.JSONObject) StyleSpan(android.text.style.StyleSpan) JSONArray(org.json.JSONArray) View(android.view.View) TextView(android.widget.TextView) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 62 with StyleSpan

use of android.text.style.StyleSpan in project WordPress-Android by wordpress-mobile.

the class DeleteSiteDialogFragment method confirmationPromptString.

private Spannable confirmationPromptString() {
    String deletePrompt = String.format(getString(R.string.confirm_delete_site_prompt), mSiteDomain);
    Spannable promptSpannable = new SpannableString(deletePrompt);
    int beginning = deletePrompt.indexOf(mSiteDomain);
    int end = beginning + mSiteDomain.length();
    promptSpannable.setSpan(new StyleSpan(Typeface.BOLD), beginning, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return promptSpannable;
}
Also used : SpannableString(android.text.SpannableString) StyleSpan(android.text.style.StyleSpan) SpannableString(android.text.SpannableString) Spannable(android.text.Spannable)

Example 63 with StyleSpan

use of android.text.style.StyleSpan in project android_frameworks_base by crdroidandroid.

the class HtmlToSpannedConverter method endHeading.

private static void endHeading(Editable text) {
    // RelativeSizeSpan and StyleSpan are CharacterStyles
    // Their ranges should not include the newlines at the end
    Heading h = getLast(text, Heading.class);
    if (h != null) {
        setSpanFromMark(text, h, new RelativeSizeSpan(HEADING_SIZES[h.mLevel]), new StyleSpan(Typeface.BOLD));
    }
    endBlockElement(text);
}
Also used : StyleSpan(android.text.style.StyleSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan)

Example 64 with StyleSpan

use of android.text.style.StyleSpan in project android_frameworks_base by crdroidandroid.

the class TextUtilsTest method testEllipsize.

@LargeTest
public void testEllipsize() {
    CharSequence s1 = "The quick brown fox jumps over þhe lazy dog.";
    CharSequence s2 = new Wrapper(s1);
    Spannable s3 = new SpannableString(s1);
    s3.setSpan(new StyleSpan(0), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    TextPaint p = new TextPaint();
    p.setFlags(p.getFlags() & ~p.DEV_KERN_TEXT_FLAG);
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 3; j++) {
            TextUtils.TruncateAt kind = null;
            switch(j) {
                case 0:
                    kind = TextUtils.TruncateAt.START;
                    break;
                case 1:
                    kind = TextUtils.TruncateAt.END;
                    break;
                case 2:
                    kind = TextUtils.TruncateAt.MIDDLE;
                    break;
            }
            String out1 = TextUtils.ellipsize(s1, p, i, kind).toString();
            String out2 = TextUtils.ellipsize(s2, p, i, kind).toString();
            String out3 = TextUtils.ellipsize(s3, p, i, kind).toString();
            String keep1 = TextUtils.ellipsize(s1, p, i, kind, true, null).toString();
            String keep2 = TextUtils.ellipsize(s2, p, i, kind, true, null).toString();
            String keep3 = TextUtils.ellipsize(s3, p, i, kind, true, null).toString();
            String trim1 = keep1.replace("", "");
            // Are all normal output strings identical?
            assertEquals("wid " + i + " pass " + j, out1, out2);
            assertEquals("wid " + i + " pass " + j, out2, out3);
            // Are preserved output strings identical?
            assertEquals("wid " + i + " pass " + j, keep1, keep2);
            assertEquals("wid " + i + " pass " + j, keep2, keep3);
            // Does trimming padding from preserved yield normal?
            assertEquals("wid " + i + " pass " + j, out1, trim1);
            // Did preserved output strings preserve length?
            assertEquals("wid " + i + " pass " + j, keep1.length(), s1.length());
            // Does the output string actually fit in the space?
            assertTrue("wid " + i + " pass " + j, p.measureText(out1) <= i);
            // Is the padded output the same width as trimmed output?
            assertTrue("wid " + i + " pass " + j, p.measureText(keep1) == p.measureText(out1));
        }
    }
}
Also used : StyleSpan(android.text.style.StyleSpan) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 65 with StyleSpan

use of android.text.style.StyleSpan in project android_frameworks_base by crdroidandroid.

the class ProgressDialog method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    TypedArray a = mContext.obtainStyledAttributes(null, com.android.internal.R.styleable.AlertDialog, com.android.internal.R.attr.alertDialogStyle, 0);
    if (mProgressStyle == STYLE_HORIZONTAL) {
        /* Use a separate handler to update the text views as they
             * must be updated on the same thread that created them.
             */
        mViewUpdateHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                /* Update the number and percent */
                int progress = mProgress.getProgress();
                int max = mProgress.getMax();
                if (mProgressNumberFormat != null) {
                    String format = mProgressNumberFormat;
                    mProgressNumber.setText(String.format(format, progress, max));
                } else {
                    mProgressNumber.setText("");
                }
                if (mProgressPercentFormat != null) {
                    double percent = (double) progress / (double) max;
                    SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
                    tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mProgressPercent.setText(tmp);
                } else {
                    mProgressPercent.setText("");
                }
            }
        };
        View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout, R.layout.alert_dialog_progress), null);
        mProgress = (ProgressBar) view.findViewById(R.id.progress);
        mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
        mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
        setView(view);
    } else {
        View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_progressLayout, R.layout.progress_dialog), null);
        mProgress = (ProgressBar) view.findViewById(R.id.progress);
        mMessageView = (TextView) view.findViewById(R.id.message);
        setView(view);
    }
    a.recycle();
    if (mMax > 0) {
        setMax(mMax);
    }
    if (mProgressVal > 0) {
        setProgress(mProgressVal);
    }
    if (mSecondaryProgressVal > 0) {
        setSecondaryProgress(mSecondaryProgressVal);
    }
    if (mIncrementBy > 0) {
        incrementProgressBy(mIncrementBy);
    }
    if (mIncrementSecondaryBy > 0) {
        incrementSecondaryProgressBy(mIncrementSecondaryBy);
    }
    if (mProgressDrawable != null) {
        setProgressDrawable(mProgressDrawable);
    }
    if (mIndeterminateDrawable != null) {
        setIndeterminateDrawable(mIndeterminateDrawable);
    }
    if (mMessage != null) {
        setMessage(mMessage);
    }
    setIndeterminate(mIndeterminate);
    onProgressChanged();
    super.onCreate(savedInstanceState);
}
Also used : SpannableString(android.text.SpannableString) Message(android.os.Message) LayoutInflater(android.view.LayoutInflater) TypedArray(android.content.res.TypedArray) StyleSpan(android.text.style.StyleSpan) Handler(android.os.Handler) SpannableString(android.text.SpannableString) TextView(android.widget.TextView) View(android.view.View)

Aggregations

StyleSpan (android.text.style.StyleSpan)143 SpannableString (android.text.SpannableString)56 SpannableStringBuilder (android.text.SpannableStringBuilder)34 RelativeSizeSpan (android.text.style.RelativeSizeSpan)32 ForegroundColorSpan (android.text.style.ForegroundColorSpan)25 View (android.view.View)18 TextView (android.widget.TextView)17 UnderlineSpan (android.text.style.UnderlineSpan)16 Spannable (android.text.Spannable)14 StrikethroughSpan (android.text.style.StrikethroughSpan)13 TypefaceSpan (android.text.style.TypefaceSpan)12 TypedArray (android.content.res.TypedArray)10 TextPaint (android.text.TextPaint)10 SubscriptSpan (android.text.style.SubscriptSpan)10 SuperscriptSpan (android.text.style.SuperscriptSpan)10 Handler (android.os.Handler)9 Message (android.os.Message)9 URLSpan (android.text.style.URLSpan)8 JustifiedSpan (com.bluejamesbond.text.style.JustifiedSpan)8 ArticleBuilder (com.bluejamesbond.text.util.ArticleBuilder)8