Xamarin.forms: The Multilinelabel Doesn't Longer Work On Android
On my Xamarin.Forms project, I use a MultiLineLabel to display a title on 1 or 2 lines, depending the text length. I'm based on this blog to achieve this. So I have a MultiLineLabe
Solution 1:
But after having updated to the latest version (Xamarin.Forms v.2.4.0.269-pre2), it doesn't longer work as expected:
Cause:
I've checked the source codes of Xamarin.Forms v.2.4.0.269-pre2
. In LabelRenderer
's OnElementChange
event, FormsTextView
's SetLineBreakMode
will be called which contains following codes:
publicstaticvoidSetLineBreakMode(this TextView textView, LineBreakMode lineBreakMode)
{
switch (lineBreakMode)
{
case LineBreakMode.NoWrap:
textView.SetMaxLines(1);
textView.SetSingleLine(true);
textView.Ellipsize = null;
break;
case LineBreakMode.WordWrap:
textView.Ellipsize = null;
textView.SetMaxLines(100);
textView.SetSingleLine(false);
break;
case LineBreakMode.CharacterWrap:
textView.Ellipsize = null;
textView.SetMaxLines(100);
textView.SetSingleLine(false);
break;
case LineBreakMode.HeadTruncation:
textView.SetMaxLines(1);
textView.SetSingleLine(true);
textView.Ellipsize = TextUtils.TruncateAt.Start;
break;
case LineBreakMode.TailTruncation:
textView.SetMaxLines(1);
textView.SetSingleLine(true);
textView.Ellipsize = TextUtils.TruncateAt.End;
break;
case LineBreakMode.MiddleTruncation:
textView.SetMaxLines(1);
textView.SetSingleLine(true);
textView.Ellipsize = TextUtils.TruncateAt.Middle;
break;
}
}
As you can see, if you use LineBreakMode.TailTruncation
, textView.SetMaxLines(1);
and textView.SetSingleLine(true);
will be called, which disable the multi line function.(In 2.3.4
textView.SetSingleLine(true);
doesn't exist).
Answer :
To fix the problem, you simply need to add two lines of codes in your renderer:
protectedoverridevoidOnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Label> e)
{
base.OnElementChanged(e);
MultiLineLabel multiLineLabel = (MultiLineLabel)Element;
if (multiLineLabel != null && multiLineLabel.Lines != -1)
{
Control.SetSingleLine(false);
Control.SetMaxLines(multiLineLabel.Lines);
Control.SetLines(multiLineLabel.Lines);
}
}
Post a Comment for "Xamarin.forms: The Multilinelabel Doesn't Longer Work On Android"