Skip to content

Instantly share code, notes, and snippets.

@vitkidd
Created May 3, 2021 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitkidd/d9c57dc26fb28fd4ca66c5299ed334ee to your computer and use it in GitHub Desktop.
Save vitkidd/d9c57dc26fb28fd4ca66c5299ed334ee to your computer and use it in GitHub Desktop.
span.java
private void updateView(int blockPosition, boolean needPlaceHolder) {
Block block = blocks.get(blockPosition);
String imageUrl = block.getView() == null ? null : block.getView().getImageUrl();
String title = block.getView() == null ? null : (block.getView().getTitle());
String description = block.getView() == null ? null : block.getView().getDescription();
String padding = "\n\n";
if (title == null) {
title = "";
}
if (description == null) {
description = "";
}
int titleL = title.length();
int paddingL = padding.length();
int descriptionL = description.length();
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(title+padding+description);
// Set the custom typeface and font size to span over a section of the spannable object
spannable.setSpan(new CustomTypefaceSpan("", mediumTypeface), 0, titleL, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new AbsoluteSizeSpan(sp24), 0, titleL, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new CustomTypefaceSpan("", regularTypeface), titleL, titleL + paddingL, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new AbsoluteSizeSpan(sp12), titleL, titleL + paddingL, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new CustomTypefaceSpan("", regularTypeface), titleL + paddingL, titleL + paddingL + descriptionL, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new AbsoluteSizeSpan(sp16), titleL + paddingL, titleL + paddingL + descriptionL, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textTv.setText(spannable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment