Bug 59218 - [Android] MaxLines does not work if LineBreakMode is set to truncation option
Summary: [Android] MaxLines does not work if LineBreakMode is set to truncation option
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.4.0
Hardware: PC Windows
: Normal normal
Target Milestone: 2.4.0
Assignee: Samantha Houts [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-09-04 18:56 UTC by Pierre-Christophe Dus
Modified: 2017-09-13 00:20 UTC (History)
3 users (show)

Tags: ac android textview label linebreakmode maxlines
Is this bug a regression?: Yes
Last known good build: 2.3.4.270


Attachments
Screenshot comparaison (457.36 KB, image/png)
2017-09-04 18:56 UTC, Pierre-Christophe Dus
Details
repro project (48.88 KB, application/x-zip-compressed)
2017-09-05 19:48 UTC, Jimmy [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Pierre-Christophe Dus 2017-09-04 18:56:08 UTC
Created attachment 24561 [details]
Screenshot comparaison

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 MultiLineLabel control:
public class MultiLineLabel : Label
{
    private static int _defaultLineSetting = -1;

    public static readonly BindableProperty LinesProperty = BindableProperty.Create(nameof(Lines), typeof(int), typeof(MultiLineLabel), _defaultLineSetting);
    public int Lines
    {
        get { return (int)GetValue(LinesProperty); }
        set { SetValue(LinesProperty, value); }
    }
}

I use this renderer on Android:
public class CustomMultiLineLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        MultiLineLabel multiLineLabel = (MultiLineLabel)Element;

        if (multiLineLabel != null && multiLineLabel.Lines != -1)
        {
            Control.Ellipsize = TextUtils.TruncateAt.End;
            Control.SetMaxLines(multiLineLabel.Lines);
        }
    }
}

And I use the MultiLineLabel in XAML like this:
<StackLayout 
    Grid.Row="0"
    Spacing="0">

    <local:MultiLineLabel
        Margin="8,6,8,0"
        TextColor="{ DynamicResource InverseTextColor }"
        Text="{ Binding encart_titre }"
        FontSize="{ artina:OnOrientationDouble 
            Default=16,
            PortraitTablet=20,
            LandscapeTablet=20 }"
        LineBreakMode="TailTruncation"
        Lines="2"
        Grid.Column="0"
        BackgroundColor="Yellow"
    />
</StackLayout>

Until I used Xamarin.Forms v.2.3.4.247, this worked well on Android. But after having updated to the latest version (Xamarin.Forms v.2.4.0.269-pre2), it doesn't longer work as expected. [see screenshots]
Comment 1 Jimmy [MSFT] 2017-09-05 19:48:35 UTC
Created attachment 24582 [details]
repro project

### Overview
Using a custom renderer to set MaxLines on a TextView has no effect when the Label has LineBreakMode set to one of the truncation options.


### Steps to Reproduce
1. Run the attached project


### Expected Results
Both Labels will truncate after two lines


### Actual Results
The first Label does not set a LineBreakMode and is truncated after two lines. However the second Label is set to TailTruncation and it truncates at one line.


### Workaround
Use the default LineBreakMode setting (WordWrap)


### Version Test
2.4.0-pre2  BAD
2.3.4.270   GOOD
Comment 2 Samantha Houts [MSFT] 2017-09-13 00:20:53 UTC
In https://github.com/xamarin/Xamarin.Forms/pull/932, we fixed a bug that caused the ellipsis calculation to be incorrect by adding Control.SetSingleLine(true) when using TailTruncation. We expect the MaxLines to be set to 1, so this makes sense.

If you would like to override the MaxLines in your renderer, please also override SetSingleLine. For example:

		protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
		{
			base.OnElementChanged(e);

			if (Control != null)
			{
				Control.SetSingleLine(false);
				Control.SetMaxLines(2);
			}
		}

Please note that you need to set SetSingleLine *first*.

Please let us know if you have any further questions.

Thank you!