Skip to content

Instantly share code, notes, and snippets.

View glureau's full-sized avatar
🏡

Grégory Lureau glureau

🏡
View GitHub Profile
/**
* Method to adjust (auto-size) the text size AND the images height.
* This approach is based on width and lines instead of height, so you can use layout_heigt="wrap_content"
* and the TextView height will adapt to the best font size (instead of hardcoded heights).
* Idea of improvements: use layout_height value if hardcoded, and use current behaviour for match_parent or wrap_content.
*
* /!\ WARNING /!\
* You can define the min/max/granularity of autoSize with the standard appCompat definitions BUT you have to define the type to uniform.
* app:autoSizeTextType="uniform"
* app:autoSizeMaxTextSize="500sp"
interface DrawableHeightComputeMode {
fun computeHeight(textPaint: TextPaint, text: CharSequence): Int
}
fun TextView.alignImageToText(textPaint: TextPaint, drawableHeightComputer: DrawableHeightComputeMode) {
(text as? SpannedString)?.getSpans(0, text.length, DynamicDrawableSpan::class.java)?.forEach {
val drawable = it.drawable
val ratio = drawable.intrinsicWidth.toFloat() / drawable.intrinsicHeight
val fontHeight = drawableHeightComputer.computeHeight(textPaint, text)
val drawableWidth = truncate(fontHeight * ratio).toInt()
private boolean suggestedSizeFitsInSpace(int suggestedSizeInPx, RectF availableSpace) {
CharSequence text = mTextView.getText();
TransformationMethod transformationMethod = mTextView.getTransformationMethod();
if (transformationMethod != null) {
CharSequence transformedText = transformationMethod.getTransformation(text, mTextView);
if (transformedText != null) {
text = transformedText;
}
}
/**********************************************************************************************************************
* Remapping auto-size functionalities from AppCompatTextViewAutoSizeHelper.
**********************************************************************************************************************/
fun AppCompatTextView.autoSizeTextAvailableSizes(): IntArray =
invokeAndReturnWithDefault(this, "getAutoSizeTextAvailableSizes", IntArray(0))
/**********************************************************************************************************************
* Copied from AppCompatTextViewAutoSizeHelper but adapted to AppCompatTextView.
class MediumActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_medium)
tv3.text = "FOR <picto> DEVS"
val drawable = ContextCompat.getDrawable(this, R.drawable.medium_logo_lowres)!!.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"