Skip to content

Commit

Permalink
feat(android): add autoSize to label (#13245)
Browse files Browse the repository at this point in the history
* feat(android): add autoSize to label

* updateProperty

* Update Label.yml

Co-authored-by: Hans Knöchel <hansemannn@users.noreply.github.com>
  • Loading branch information
m1ga and hansemannn committed Aug 29, 2022
1 parent 86c7c20 commit ea1095f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
Expand Up @@ -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";

Expand Down
11 changes: 11 additions & 0 deletions apidoc/Titanium/UI/Label.yml
Expand Up @@ -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
Expand Down

0 comments on commit ea1095f

Please sign in to comment.