Search in sources :

Example 21 with CharacterStyle

use of android.text.style.CharacterStyle in project Anki-Android by Ramblurr.

the class CompatV11 method setSubtitle.

@Override
public void setSubtitle(Activity activity, String title, boolean inverted) {
    ActionBar ab = activity.getActionBar();
    if (ab != null) {
        if (inverted) {
            CharacterStyle span = new ForegroundColorSpan(activity.getResources().getColor(inverted ? R.color.white : R.color.black));
            SpannableStringBuilder ssb = new SpannableStringBuilder(title);
            ssb.setSpan(span, 0, ssb.length(), 0);
            ab.setSubtitle(ssb);
        } else {
            ab.setSubtitle(title);
        }
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) ActionBar(android.app.ActionBar) CharacterStyle(android.text.style.CharacterStyle) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 22 with CharacterStyle

use of android.text.style.CharacterStyle in project Anki-Android by Ramblurr.

the class CompatV11 method setTitle.

@Override
public void setTitle(Activity activity, String title, boolean inverted) {
    ActionBar ab = activity.getActionBar();
    if (ab != null) {
        CharacterStyle span = new ForegroundColorSpan(activity.getResources().getColor(inverted ? R.color.white : R.color.black));
        SpannableStringBuilder ssb = new SpannableStringBuilder(title);
        ssb.setSpan(span, 0, ssb.length(), 0);
        ab.setTitle(ssb);
    }
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) ActionBar(android.app.ActionBar) CharacterStyle(android.text.style.CharacterStyle) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 23 with CharacterStyle

use of android.text.style.CharacterStyle in project Jota-Text-Editor-old by jiro-aqua.

the class Styled method drawUniformRun.

/**
 * Draws and/or measures a uniform run of text on a single line. No span of
 * interest should start or end in the middle of this run (if not
 * drawing, character spans that don't affect metrics can be ignored).
 * Neither should the run direction change in the middle of the run.
 *
 * <p>The x position is the leading edge of the text. In a right-to-left
 * paragraph, this will be to the right of the text to be drawn. Paint
 * should not have an Align value other than LEFT or positioning will get
 * confused.
 *
 * <p>On return, workPaint will reflect the original paint plus any
 * modifications made by character styles on the run.
 *
 * <p>The returned width is signed and will be < 0 if the paragraph
 * direction is right-to-left.
 */
private static float drawUniformRun(Canvas canvas, Spanned text, int start, int end, int dir, boolean runIsRtl, float x, int top, int y, int bottom, Paint.FontMetricsInt fmi, TextPaint paint, TextPaint workPaint, boolean needWidth) {
    boolean haveWidth = false;
    float ret = 0;
    CharacterStyle[] spans = text.getSpans(start, end, CharacterStyle.class);
    ReplacementSpan replacement = null;
    // XXX: This shouldn't be modifying paint, only workPaint.
    // However, the members belonging to TextPaint should have default
    // values anyway.  Better to ensure this in the Layout constructor.
    paint.bgColor = 0;
    paint.baselineShift = 0;
    workPaint.set(paint);
    if (spans.length > 0) {
        for (int i = 0; i < spans.length; i++) {
            CharacterStyle span = spans[i];
            if (span instanceof ReplacementSpan) {
                replacement = (ReplacementSpan) span;
            } else {
                span.updateDrawState(workPaint);
            }
        }
    }
    if (replacement == null) {
        CharSequence tmp;
        int tmpstart, tmpend;
        if (runIsRtl) {
            tmp = TextUtils.getReverse(text, start, end);
            tmpstart = 0;
            // XXX: assumes getReverse doesn't change the length of the text
            tmpend = end - start;
        } else {
            tmp = text;
            tmpstart = start;
            tmpend = end;
        }
        if (fmi != null) {
            workPaint.getFontMetricsInt(fmi);
        }
        if (canvas != null) {
            if (workPaint.bgColor != 0) {
                int c = workPaint.getColor();
                Paint.Style s = workPaint.getStyle();
                workPaint.setColor(workPaint.bgColor);
                workPaint.setStyle(Paint.Style.FILL);
                if (!haveWidth) {
                    ret = workPaint.measureText(tmp, tmpstart, tmpend);
                    haveWidth = true;
                }
                if (dir == Layout.DIR_RIGHT_TO_LEFT)
                    canvas.drawRect(x - ret, top, x, bottom, workPaint);
                else
                    canvas.drawRect(x, top, x + ret, bottom, workPaint);
                workPaint.setStyle(s);
                workPaint.setColor(c);
            }
            if (dir == Layout.DIR_RIGHT_TO_LEFT) {
                if (!haveWidth) {
                    ret = workPaint.measureText(tmp, tmpstart, tmpend);
                    haveWidth = true;
                }
                canvas.drawText(tmp, tmpstart, tmpend, x - ret, y + workPaint.baselineShift, workPaint);
            } else {
                if (needWidth) {
                    if (!haveWidth) {
                        ret = workPaint.measureText(tmp, tmpstart, tmpend);
                        haveWidth = true;
                    }
                }
                canvas.drawText(tmp, tmpstart, tmpend, x, y + workPaint.baselineShift, workPaint);
            }
        } else {
            if (needWidth && !haveWidth) {
                ret = workPaint.measureText(tmp, tmpstart, tmpend);
                haveWidth = true;
            }
        }
    } else {
        ret = replacement.getSize(workPaint, text, start, end, fmi);
        if (canvas != null) {
            if (dir == Layout.DIR_RIGHT_TO_LEFT)
                replacement.draw(canvas, text, start, end, x - ret, top, y, bottom, workPaint);
            else
                replacement.draw(canvas, text, start, end, x, top, y, bottom, workPaint);
        }
    }
    if (dir == Layout.DIR_RIGHT_TO_LEFT)
        return -ret;
    else
        return ret;
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) ReplacementSpan(jp.sblo.pandora.jota.text.style.ReplacementSpan) CharacterStyle(android.text.style.CharacterStyle) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 24 with CharacterStyle

use of android.text.style.CharacterStyle in project TextJustify-Android by bluejamesbond.

the class Styled method drawUniformRun.

/**
 * Draws and/or measures a uniform run of text on a single line. No span of
 * interest should start or end in the middle of this run (if not
 * drawing, character spans that don't affect metrics can be ignored).
 * Neither should the run direction change in the middle of the run.
 *
 * The x position is the leading edge of the text. In a right-to-left
 * paragraph, this will be to the right of the text to be drawn. Paint
 * should not have an Align value other than LEFT or positioning will isCancelled
 * confused.
 *
 * On return, workPaint will reflect the original paint plus any
 * modifications made by character styles on the run.
 *
 * The returned width is signed and will be < 0 if the paragraph
 * direction is right-to-left.
 */
private static float drawUniformRun(Canvas canvas, Spanned text, int start, int end, int dir, boolean runIsRtl, float x, int top, int y, int bottom, Paint.FontMetricsInt fmi, TextPaint paint, TextPaint workPaint, boolean needWidth) {
    boolean haveWidth = false;
    float ret = 0;
    CharacterStyle[] spans = text.getSpans(start, end, CharacterStyle.class);
    ReplacementSpan replacement = null;
    // XXX: This shouldn't be modifying paint, only workPaint.
    // However, the members belonging to TextPaint should have default
    // values anyway.  Better to ensure this in the Layout constructor.
    paint.bgColor = 0;
    paint.baselineShift = 0;
    workPaint.set(paint);
    if (spans.length > 0) {
        for (CharacterStyle span : spans) {
            if (span instanceof ReplacementSpan) {
                replacement = (ReplacementSpan) span;
            } else {
                span.updateDrawState(workPaint);
            }
        }
    }
    if (replacement == null) {
        CharSequence tmp;
        int tmpstart, tmpend;
        if (runIsRtl) {
            tmp = TextUtils.getReverse(text, start, end);
            tmpstart = 0;
            // XXX: assumes getReverse doesn't change the length of the text
            tmpend = end - start;
        } else {
            tmp = text;
            tmpstart = start;
            tmpend = end;
        }
        if (fmi != null) {
            workPaint.getFontMetricsInt(fmi);
        }
        if (canvas != null) {
            if (workPaint.bgColor != 0) {
                int c = workPaint.getColor();
                Paint.Style s = workPaint.getStyle();
                workPaint.setColor(workPaint.bgColor);
                workPaint.setStyle(Paint.Style.FILL);
                if (!haveWidth) {
                    ret = workPaint.measureText(tmp, tmpstart, tmpend);
                    haveWidth = true;
                }
                if (dir == Layout.DIR_RIGHT_TO_LEFT) {
                    canvas.drawRect(x - ret, top, x, bottom, workPaint);
                } else {
                    canvas.drawRect(x, top, x + ret, bottom, workPaint);
                }
                workPaint.setStyle(s);
                workPaint.setColor(c);
            }
            if (dir == Layout.DIR_RIGHT_TO_LEFT) {
                if (!haveWidth) {
                    ret = workPaint.measureText(tmp, tmpstart, tmpend);
                    haveWidth = true;
                }
                canvas.drawText(tmp, tmpstart, tmpend, x - ret, y + workPaint.baselineShift, workPaint);
            } else {
                if (needWidth) {
                    if (!haveWidth) {
                        ret = workPaint.measureText(tmp, tmpstart, tmpend);
                        haveWidth = true;
                    }
                }
                canvas.drawText(tmp, tmpstart, tmpend, x, y + workPaint.baselineShift, workPaint);
            }
        } else {
            if (needWidth && !haveWidth) {
                ret = workPaint.measureText(tmp, tmpstart, tmpend);
                haveWidth = true;
            }
        }
    } else {
        ret = replacement.getSize(workPaint, text, start, end, fmi);
        if (canvas != null) {
            if (dir == Layout.DIR_RIGHT_TO_LEFT) {
                replacement.draw(canvas, text, start, end, x - ret, top, y, bottom, workPaint);
            } else {
                replacement.draw(canvas, text, start, end, x, top, y, bottom, workPaint);
            }
        }
    }
    if (dir == Layout.DIR_RIGHT_TO_LEFT) {
        return -ret;
    } else {
        return ret;
    }
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) ReplacementSpan(android.text.style.ReplacementSpan) CharacterStyle(android.text.style.CharacterStyle) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 25 with CharacterStyle

use of android.text.style.CharacterStyle in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class RichInputConnection method commitText.

/**
 * Calls {@link InputConnection#commitText(CharSequence, int)}.
 *
 * @param text The text to commit. This may include styles.
 * @param newCursorPosition The new cursor position around the text.
 */
public void commitText(final CharSequence text, final int newCursorPosition) {
    if (DEBUG_BATCH_NESTING)
        checkBatchEdit();
    if (DEBUG_PREVIOUS_TEXT)
        checkConsistencyForDebug();
    mCommittedTextBeforeComposingText.append(text);
    // TODO: the following is exceedingly error-prone. Right now when the cursor is in the
    // middle of the composing word mComposingText only holds the part of the composing text
    // that is before the cursor, so this actually works, but it's terribly confusing. Fix this.
    mExpectedSelStart += text.length() - mComposingText.length();
    mExpectedSelEnd = mExpectedSelStart;
    mComposingText.setLength(0);
    if (isConnected()) {
        mTempObjectForCommitText.clear();
        mTempObjectForCommitText.append(text);
        final CharacterStyle[] spans = mTempObjectForCommitText.getSpans(0, text.length(), CharacterStyle.class);
        for (final CharacterStyle span : spans) {
            final int spanStart = mTempObjectForCommitText.getSpanStart(span);
            final int spanEnd = mTempObjectForCommitText.getSpanEnd(span);
            final int spanFlags = mTempObjectForCommitText.getSpanFlags(span);
            // See https://b.corp.google.com/issues/19255233
            if (0 < spanEnd && spanEnd < mTempObjectForCommitText.length()) {
                final char spanEndChar = mTempObjectForCommitText.charAt(spanEnd - 1);
                final char nextChar = mTempObjectForCommitText.charAt(spanEnd);
                if (UnicodeSurrogate.isLowSurrogate(spanEndChar) && UnicodeSurrogate.isHighSurrogate(nextChar)) {
                    mTempObjectForCommitText.setSpan(span, spanStart, spanEnd + 1, spanFlags);
                }
            }
        }
        mIC.commitText(mTempObjectForCommitText, newCursorPosition);
    }
}
Also used : CharacterStyle(android.text.style.CharacterStyle)

Aggregations

CharacterStyle (android.text.style.CharacterStyle)58 SpannableStringBuilder (android.text.SpannableStringBuilder)22 ForegroundColorSpan (android.text.style.ForegroundColorSpan)18 RelativeSizeSpan (android.text.style.RelativeSizeSpan)18 Paint (android.graphics.Paint)16 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)14 StyleSpan (android.text.style.StyleSpan)13 TextPaint (android.text.TextPaint)12 ParcelableSpan (android.text.ParcelableSpan)9 SpannableString (android.text.SpannableString)9 Spanned (android.text.Spanned)9 StrikethroughSpan (android.text.style.StrikethroughSpan)9 TypefaceSpan (android.text.style.TypefaceSpan)9 UnderlineSpan (android.text.style.UnderlineSpan)9 BackgroundColorSpan (android.text.style.BackgroundColorSpan)8 ReplacementSpan (android.text.style.ReplacementSpan)8 SubscriptSpan (android.text.style.SubscriptSpan)8 SuperscriptSpan (android.text.style.SuperscriptSpan)8 URLSpan (android.text.style.URLSpan)8 ImageSpan (android.text.style.ImageSpan)7