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 478bcfd7b3f..61e2ab2d739 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 @@ -60,6 +60,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 717787d6b9d..bc765e17d17 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 @@ -799,4 +799,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(); + } } diff --git a/apidoc/Titanium/UI/Label.yml b/apidoc/Titanium/UI/Label.yml index e8f9496cbf3..d1eee23015e 100644 --- a/apidoc/Titanium/UI/Label.yml +++ b/apidoc/Titanium/UI/Label.yml @@ -175,6 +175,12 @@ 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`. + type: Number + permission: read-only + 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`. platforms: [android] @@ -247,6 +253,13 @@ 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.3.0"} + - name: wordWrap summary: Enable or disable word wrapping in the label. type: Boolean 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