From cea5d2b26ba411acb92a1840c5e96c8ae0fd650f Mon Sep 17 00:00:00 2001 From: m1ga Date: Thu, 29 Sep 2022 22:44:07 +0200 Subject: [PATCH 1/4] feat(android): lineCount/visibleText for Ti.UI.Label --- .../ti/modules/titanium/ui/LabelProxy.java | 21 +++++++++++++++++++ .../modules/titanium/ui/widget/TiUILabel.java | 12 +++++++++++ 2 files changed, 33 insertions(+) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java index 7032e50f1c6..12f7acda584 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java @@ -13,6 +13,7 @@ import org.appcelerator.titanium.view.TiUIView; import ti.modules.titanium.ui.widget.TiUILabel; + import android.app.Activity; @Kroll.proxy(creatableInModule = UIModule.class, @@ -58,6 +59,26 @@ protected KrollDict getLangConversionTable() return table; } + @Kroll.getProperty + public int getLineCount() + { + TiUIView v = getOrCreateView(); + if (v instanceof TiUILabel) { + return ((TiUILabel) v).getLineCount(); + } + return 0; + } + + @Kroll.getProperty + public String getVisibleText() + { + TiUIView v = getOrCreateView(); + if (v instanceof TiUILabel) { + return ((TiUILabel) v).getVisibleText(); + } + return ""; + } + @Override public TiUIView createView(Activity activity) { 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 374de058775..db502967f4e 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 @@ -777,4 +777,16 @@ private void updateLabelText() textView.setText(text, MaterialTextView.BufferType.NORMAL); textView.requestLayout(); } + + public int getLineCount() + { + MaterialTextView textView = (MaterialTextView) getNativeView(); + return textView.getLineCount(); + } + + public String getVisibleText() + { + MaterialTextView textView = (MaterialTextView) getNativeView(); + return textView.getLayout().getText().toString(); + } } From 6d8b2b58d79294c240bed192f47a9e24a5096900 Mon Sep 17 00:00:00 2001 From: m1ga Date: Thu, 29 Sep 2022 22:55:26 +0200 Subject: [PATCH 2/4] readme --- .../src/java/ti/modules/titanium/ui/LabelProxy.java | 1 - apidoc/Titanium/UI/Label.yml | 13 +++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java index 12f7acda584..e6cdfe91c88 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java @@ -13,7 +13,6 @@ import org.appcelerator.titanium.view.TiUIView; import ti.modules.titanium.ui.widget.TiUILabel; - import android.app.Activity; @Kroll.proxy(creatableInModule = UIModule.class, diff --git a/apidoc/Titanium/UI/Label.yml b/apidoc/Titanium/UI/Label.yml index b584597419e..7fa7fca2a14 100644 --- a/apidoc/Titanium/UI/Label.yml +++ b/apidoc/Titanium/UI/Label.yml @@ -152,6 +152,13 @@ properties: type: Number since: "4.1.0" + - name: lineCount + summary: Returns the amount of lines the content is acually using. Is equal or lower than `maxLines`. + platforms: [android] + type: Number + permission: read-only + since: {android: "12.0.0"} + - name: lineSpacing summary: Line spacing of the [text](Titanium.UI.Label.text), as a dictionary with the properties `add` and `multiply`. platforms: [android] @@ -224,6 +231,12 @@ properties: description: Only one of `text` or `textid` should be specified. type: String + - name: visibleText + summary: Returns the actual text seen on the screen. If the text is ellipsized it will be different to the normal `text`. + platforms: [android] + type: String + permission: read-only + since: {android: "12.0.0"} - name: wordWrap summary: Enable or disable word wrapping in the label. From 206c42e386c0bd1751f61f28d0c28ecd7b0218f8 Mon Sep 17 00:00:00 2001 From: Michael Gangolf Date: Mon, 25 Dec 2023 11:47:34 +0100 Subject: [PATCH 3/4] Update apidoc/Titanium/UI/Label.yml --- apidoc/Titanium/UI/Label.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apidoc/Titanium/UI/Label.yml b/apidoc/Titanium/UI/Label.yml index 2614bc50c84..6d4e06f39e2 100644 --- a/apidoc/Titanium/UI/Label.yml +++ b/apidoc/Titanium/UI/Label.yml @@ -180,7 +180,7 @@ properties: platforms: [android] type: Number permission: read-only - since: {android: "12.0.0"} + since: {android: "12.3.0"} - name: lineSpacing summary: Line spacing of the [text](Titanium.UI.Label.text), as a dictionary with the properties `add` and `multiply`. From 6268b09ede921b05b4e9cb9aa010d9fa61182853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hans=20Kn=C3=B6chel?= Date: Mon, 25 Dec 2023 14:27:58 +0100 Subject: [PATCH 4/4] feat(ios): add lineCount --- apidoc/Titanium/UI/Label.yml | 3 +-- iphone/Classes/TiUILabelProxy.m | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apidoc/Titanium/UI/Label.yml b/apidoc/Titanium/UI/Label.yml index 6d4e06f39e2..d1eee23015e 100644 --- a/apidoc/Titanium/UI/Label.yml +++ b/apidoc/Titanium/UI/Label.yml @@ -177,10 +177,9 @@ properties: - name: lineCount summary: Returns the amount of lines the content is acually using. Is equal or lower than `maxLines`. - platforms: [android] type: Number permission: read-only - since: {android: "12.3.0"} + since: "12.3.0" - name: lineSpacing summary: Line spacing of the [text](Titanium.UI.Label.text), as a dictionary with the properties `add` and `multiply`. diff --git a/iphone/Classes/TiUILabelProxy.m b/iphone/Classes/TiUILabelProxy.m index 8a9f08a042f..864696d518b 100644 --- a/iphone/Classes/TiUILabelProxy.m +++ b/iphone/Classes/TiUILabelProxy.m @@ -86,6 +86,23 @@ - (NSNumber *)ellipsize return NUMINTEGER([[(TiUILabel *)[self view] label] lineBreakMode]); } +- (NSNumber *)lineCount +{ + UILabel *label = [(TiUILabel *)[self view] label]; + + CGSize maxSize = CGSizeMake(label.frame.size.width, MAXFLOAT); + NSString *text = label.text ?: @""; + CGFloat textHeight = [text boundingRectWithSize:maxSize + options:NSStringDrawingUsesLineFragmentOrigin + attributes:@{ NSFontAttributeName : label.font } + context:nil] + .size.height; + CGFloat lineHeight = label.font.lineHeight; + NSNumber *lineCount = NUMINT(ceil(textHeight / lineHeight)); + + return lineCount; +} + @end #endif