diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUILabel.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUILabel.java index 4723d8e1f85..374de058775 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUILabel.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUILabel.java @@ -37,6 +37,8 @@ import android.view.Gravity; import android.view.MotionEvent; import androidx.annotation.NonNull; +import androidx.core.widget.TextViewCompat; + import com.google.android.material.textview.MaterialTextView; public class TiUILabel extends TiUIView @@ -58,6 +60,7 @@ public class TiUILabel extends TiUIView private float unscaledFontSizeInPixels = -1.0f; private CharSequence originalText = ""; private boolean isInvalidationAndLayoutsEnabled = true; + private float oldFontSize = -1.0f; public TiUILabel(final TiViewProxy proxy) { @@ -460,6 +463,13 @@ public void processProperties(KrollDict d) tv.setShadowLayer(shadowRadius, shadowX, shadowY, shadowColor); } + if (d.containsKey(TiC.PROPERTY_AUTOSIZE)) { + if (TiConvert.toBoolean(d, TiC.PROPERTY_AUTOSIZE, false)) { + oldFontSize = tv.getTextSize(); + TextViewCompat.setAutoSizeTextTypeWithDefaults(tv, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM); + } + } + // This needs to be the last operation. updateLabelText(); tv.invalidate(); @@ -580,6 +590,18 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP if (hadFixedSize && isAutoSized) { updateLabelText(); } + } else if (key.equals(TiC.PROPERTY_AUTOSIZE)) { + if (TiConvert.toBoolean(newValue, false)) { + oldFontSize = tv.getTextSize(); + TextViewCompat.setAutoSizeTextTypeWithDefaults(tv, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM); + } else { + TextViewCompat.setAutoSizeTextTypeWithDefaults(tv, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE); + if (oldFontSize != -1) { + tv.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, oldFontSize); + tv.requestLayout(); + } + } + } else { super.propertyChanged(key, oldValue, newValue, proxy); } diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiC.java b/android/titanium/src/java/org/appcelerator/titanium/TiC.java index b31066691b5..00027eef58a 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiC.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiC.java @@ -269,6 +269,7 @@ public class TiC public static final String PROPERTY_AUTOREVERSE = "autoreverse"; public static final String PROPERTY_AUTOROTATE = "autorotate"; public static final String PROPERTY_AUTO_REDIRECT = "autoRedirect"; + public static final String PROPERTY_AUTOSIZE = "autoSize"; public static final String PROPERTY_AUTO_ENCODE_URL = "autoEncodeUrl"; public static final String PROPERTY_AUTO_TAB_TITLE = "autoTabTitle"; diff --git a/apidoc/Titanium/UI/Label.yml b/apidoc/Titanium/UI/Label.yml index 9c546ef3421..b9a089b3c39 100644 --- a/apidoc/Titanium/UI/Label.yml +++ b/apidoc/Titanium/UI/Label.yml @@ -75,6 +75,17 @@ properties: constants: Titanium.UI.AUTOLINK_* platforms: [android] + - name: autoSize + summary: Automatically scales the label into its size. + description: | + To use `autoSize` you have to set a `width` and `height` to the label and it will + automatically increase the font size to fill up the space. The actual `fontSize` + value stays the same. + type: Boolean + default: false, + platforms: [android] + since: "11.1.0" + - name: backgroundPaddingBottom summary: Number of pixels to extend the background image past the label on the bottom. type: Number