Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms - Multi-line text truncation

Truncate the label text makes it one line. App shows description, it needs to be displayed in 2-3 lines but Xamarin "LineBreakMode=TailTruncation" truncates it and restricts it to one line. Is there any way to truncate label text and show in multi-lines. If text doesn't fit into n number of lines, then it should be truncated.

<Label LineBreakMode="TailTruncation" FontSize = "20" Text="Multi line Text" />

Thanks.

like image 215
PEHLAJ Avatar asked Jun 23 '16 17:06

PEHLAJ


People also ask

What is StackLayout in Xamarin forms?

A StackLayout organizes child views in a one-dimensional stack, either horizontally or vertically. By default, a StackLayout is oriented vertically. In addition, a StackLayout can be used as a parent layout that contains other child layouts.

How do I display HTML content in Xamarin forms?

To display local content using a WebView , you'll need to open the HTML file like any other, then load the contents as a string into the Html property of an HtmlWebViewSource .


2 Answers

Since Xamarin.Forms 3.3 a new property was introduced for this feature. It's called MaxLines.

Here is an example in C#:

var yourLabel = new Label
{
    LineBreakMode = LineBreakMode.TailTruncation,
    Text = "Your text",
    MaxLines = 2
};

Here is an example in XAML:

<Label
    LineBreakMode="TailTruncation"
    Text="Your text"
    MaxLines="2" />

See https://docs.microsoft.com/de-de/dotnet/api/xamarin.forms.label.maxlines?view=xamarin-forms for more information.

like image 84
jfmg Avatar answered Sep 21 '22 06:09

jfmg


I have implemented custom renderer to handle this.

http://depblog.weblogs.us/2016/06/27/xamarin-forms-multi-line-label-custom-renderer-gotcha/

//Droid
public class MultiLineLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.LayoutChange += (s, args) =>
            {
                Control.Ellipsize = TextUtils.TruncateAt.End;
                Control.SetMaxLines(2);
            }
        };
    }
}
like image 38
PEHLAJ Avatar answered Sep 18 '22 06:09

PEHLAJ