Skip to content

Commit

Permalink
feat(android): lineCount/visibleText for Ti.UI.Label (#13583)
Browse files Browse the repository at this point in the history
* feat(android): lineCount/visibleText for Ti.UI.Label

* readme

* Update apidoc/Titanium/UI/Label.yml

* feat(ios): add lineCount

---------

Co-authored-by: Hans Knöchel <h.knoechel@lambus.com>
  • Loading branch information
m1ga and hansemannn committed Jan 1, 2024
1 parent abdc7bf commit 1eeb301
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
20 changes: 20 additions & 0 deletions android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
13 changes: 13 additions & 0 deletions apidoc/Titanium/UI/Label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions iphone/Classes/TiUILabelProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1eeb301

Please sign in to comment.