Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): lineCount/visibleText for Ti.UI.Label #13583

Merged
merged 6 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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