Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with ImageSpan alignment #3

Open
lawloretienne opened this issue Aug 31, 2018 · 1 comment
Open

Issue with ImageSpan alignment #3

lawloretienne opened this issue Aug 31, 2018 · 1 comment

Comments

@lawloretienne
Copy link

When using your library i have had issues with getting an asset to vertically align where i want it to with an ImageSpan. Do you have any workarounds for this?

@Actine
Copy link
Collaborator

Actine commented Sep 3, 2018

Hey! Sorry for not getting back earlier.

Here's what I found in some of my old code. Honestly don't remember how/why it fixed the issue, but you can give it a try:

VectorDrawableCompat d = VectorDrawableCompat.create(getResources(), R.drawable.ic_loop_vector, null);
d.mutate();
d.setTint(Color.BLACK);
d.setAlpha(192);
// Workaround to suppress line height adjustment
d.setBounds(0, -d.getIntrinsicHeight(), d.getIntrinsicWidth(), 0);

//noinspection deprecation
final CharSequence overviewCopy1Result = ViewUtils.replaceImageSpan(
        Html.fromHtml(getString(R.string.overview_copy_2)),
        new ImageSpan(d, ImageSpan.ALIGN_BASELINE)
);
overviewCopy2.setText(overviewCopy1Result);

and ViewUtils.replaceImageSpan being

public static CharSequence replaceImageSpan(CharSequence original, ImageSpan span) {
    SpannableStringBuilder builder = new SpannableStringBuilder(original);
    Object[] spans = builder.getSpans(0, builder.length(), ImageSpan.class);
    if (spans.length == 1) {
        final int start = builder.getSpanStart(spans[0]);
        final int end= builder.getSpanEnd(spans[0]);
        builder.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return builder;
}

Please let me know if it works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants