Skip to content

Instantly share code, notes, and snippets.

@erikjhordan-rey
Created November 23, 2017 17:24
Show Gist options
  • Save erikjhordan-rey/60c1cc00684583083c342b44b3fbb0da to your computer and use it in GitHub Desktop.
Save erikjhordan-rey/60c1cc00684583083c342b44b3fbb0da to your computer and use it in GitHub Desktop.
This class is an extension to facilitate the use of AutoSizing on AppCompatTextView.
mport android.support.v4.widget.TextViewCompat
import android.support.v7.widget.AppCompatTextView
import android.util.TypedValue
import android.widget.TextView
/*
* This class is an extension to facilitate the use of AutoSizing on AppCompatTextView.
*/
// Default
// Dimensions for uniform scaling are minTextSize = 12sp, maxTextSize = 112sp, and granularity = 1px.
fun AppCompatTextView.setAutoSizeTextTypeWithDefault(autoSizeTextType: Int) {
TextViewCompat.setAutoSizeTextTypeWithDefaults(this, autoSizeTextType)
}
fun AppCompatTextView.setAutoSizeTextDefaultUniform() {
TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
}
fun AppCompatTextView.setAutoSizeTextDefaultNone() {
TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE)
}
// Granularity
fun AppCompatTextView.setAutoSizeTextGranularity(autoSizeMinTextSize: Int, autoSizeMaxTextSize: Int, autoSizeStepGranularity: Int, unit: Int) {
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this, autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, unit)
}
fun AppCompatTextView.setAutoSizeTextDefaultGranularity(autoSizeMinTextSize: Int, autoSizeMaxTextSize: Int) {
val autoSizeStepGranularity = 2
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this, autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, TypedValue.COMPLEX_UNIT_SP)
}
fun AppCompatTextView.setAutoSizeTextGranularitySp(autoSizeMinTextSize: Int, autoSizeMaxTextSize: Int, autoSizeStepGranularity: Int) {
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this, autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, TypedValue.COMPLEX_UNIT_SP)
}
// Preset sizes
fun AppCompatTextView.setAutoSizeTextPresetSizes(autoSizeTextSizes: IntArray) {
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(this, autoSizeTextSizes, TypedValue.COMPLEX_UNIT_SP)
}
fun AppCompatTextView.setAutoSizeTextPresetSizes(autoSizeTextSizes: IntArray, unit: Int) {
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(this, autoSizeTextSizes, unit)
}
fun AppCompatTextView.setAutoSizeTextDefaultPresetSizes(textView: TextView) {
val autoSizeTextSizes = intArrayOf(10, 12, 16, 20, 40, 100)
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(textView, autoSizeTextSizes, TypedValue.COMPLEX_UNIT_SP)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment