Skip to content

Commit

Permalink
timob-24479 Android: fix TiUILabel maxLines not working
Browse files Browse the repository at this point in the history
  • Loading branch information
fmerzadyan committed Mar 14, 2017
1 parent 759e856 commit 1457331
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class TiUILabel extends TiUIView
private int shadowColor = Color.TRANSPARENT;
private String minimumFontSize = null;
private String autoshrinkSetFontSize = null;
private int maxLines = -1;

public TiUILabel(final TiViewProxy proxy)
{
Expand Down Expand Up @@ -229,13 +230,15 @@ public void processProperties(KrollDict d)
//it enables font scaling to fit and forces the label content to be limited to a single line.
minimumFontSize = TiConvert.toString(d, TiC.PROPERTY_MINIMUM_FONT_SIZE);
tv.setSingleLine(true);
refreshMaxLines();
tv.setEllipsize(TruncateAt.END);
}
if (d.containsKey(TiC.PROPERTY_LINES)) {
tv.setLines(TiConvert.toInt(d, TiC.PROPERTY_LINES));
}
if (d.containsKey(TiC.PROPERTY_MAX_LINES)) {
tv.setMaxLines(TiConvert.toInt(d, TiC.PROPERTY_MAX_LINES));
maxLines = TiConvert.toInt(d.get(TiC.PROPERTY_MAX_LINES));
refreshMaxLines();
}
if (d.containsKey(TiC.PROPERTY_LINE_SPACING)) {
Object value = d.get(TiC.PROPERTY_LINE_SPACING);
Expand Down Expand Up @@ -282,6 +285,7 @@ public void processProperties(KrollDict d)
case UIModule.TEXT_ELLIPSIZE_TRUNCATE_MARQUEE:
// marquee effect only works in single line mode
tv.setSingleLine(true);
refreshMaxLines();
tv.setSelected(true);
ellipsize = TruncateAt.MARQUEE; break;
default:
Expand All @@ -294,6 +298,7 @@ public void processProperties(KrollDict d)
if (d.containsKey(TiC.PROPERTY_WORD_WRAP)) {
wordWrap = TiConvert.toBoolean(d, TiC.PROPERTY_WORD_WRAP, true);
tv.setSingleLine(!wordWrap);
refreshMaxLines();
}

if (d.containsKey(TiC.PROPERTY_SHADOW_OFFSET)) {
Expand Down Expand Up @@ -329,7 +334,7 @@ public void processProperties(KrollDict d)
TiUIHelper.linkifyIfEnabled(tv, d.get(TiC.PROPERTY_AUTO_LINK));
tv.invalidate();
}

@Override
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy)
{
Expand Down Expand Up @@ -361,6 +366,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else if (key.equals(TiC.PROPERTY_MINIMUM_FONT_SIZE)) {
minimumFontSize = TiConvert.toString(newValue);
tv.setSingleLine(true);
refreshMaxLines();
tv.setEllipsize(TruncateAt.END);
tv.requestLayout();
} else if (key.equals(TiC.PROPERTY_FONT)) {
Expand All @@ -381,6 +387,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
case UIModule.TEXT_ELLIPSIZE_TRUNCATE_MARQUEE:
// marquee effect only works in single line mode
tv.setSingleLine(true);
refreshMaxLines();
tv.setSelected(true);
ellipsize = TruncateAt.MARQUEE; break;
default:
Expand All @@ -391,6 +398,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else if (key.equals(TiC.PROPERTY_WORD_WRAP)) {
wordWrap = TiConvert.toBoolean(newValue, true);
tv.setSingleLine(!wordWrap);
refreshMaxLines();
} else if (key.equals(TiC.PROPERTY_AUTO_LINK)) {
Linkify.addLinks(tv, TiConvert.toInt(newValue));
} else if (key.equals(TiC.PROPERTY_SHADOW_OFFSET)) {
Expand All @@ -409,7 +417,8 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else if (key.equals(TiC.PROPERTY_LINES)) {
tv.setLines(TiConvert.toInt(newValue));
} else if (key.equals(TiC.PROPERTY_MAX_LINES)) {
tv.setMaxLines(TiConvert.toInt(newValue));
maxLines = TiConvert.toInt(newValue);
refreshMaxLines();
} else if (key.equals(TiC.PROPERTY_ATTRIBUTED_STRING) && newValue instanceof AttributedStringProxy) {
Spannable spannableText = AttributedStringProxy.toSpannable(((AttributedStringProxy)newValue), TiApplication.getAppCurrentActivity());
if (spannableText != null) {
Expand All @@ -428,4 +437,15 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
public void setClickable(boolean clickable) {
((TextView)getNativeView()).setClickable(clickable);
}

/**
* {@link TextView#setSingleLine} method undoes the {@link TextView#setMaxLines(int)}} changes therefore to counter that problem, call this method after setSingleLine is invoked.
*/
private void refreshMaxLines()
{
TextView tv = (TextView) getNativeView();
if (tv != null && maxLines > 0) {
tv.setMaxLines(maxLines);
}
}
}

0 comments on commit 1457331

Please sign in to comment.