MaxLines on Labels in Xamarin.Forms
Hot off the press, another PR just got merged into the Xamarin.Forms repository. This time, adding a new feature on a Label, the ability to specify MaxLines. This will be available in a near-future Xamarin.Forms version on iOS, Mac OS, Android, and UWP.
Edit 21 Sep. 2018: This functionality is now available as part of Xamarin.Forms 3.3-pre1. Have a look at the release notes.
Edit 7 Dec. 2018: You can now also review this content in video: https://www.youtube.com/watch?v=8nAsv8TrFfU
What can you do with MaxLines #
With the new property MaxLines, you can specify how much lines of text a Label should show at the most. Until now, a Label in Xamarin.Forms either showed all lines or, with the relatively new LineBreakMode property, was truncated at the end of one line.
Now, with MaxLines, you can do both!
How does MaxLines work? #
It’s very easy, it’s added as a new property on the Label. Simply do something like this:
Label with a long text and the MaxLength property
The above code will now result in something like the screenshot underneath. This app is the “internal” Forms app to test all kinds of features of the Xamarin.Forms capabilities. As you might notice, not much work went into designing… ;)
MaxLines in action on iOS
You can see how the long text is now spread over two lines. Any longer text will still be in the property, but not shown on the screen. When used in conjunction with LineBreakMode you can now create a Label that is neatly cut off at the end with some ellipsis.
MaxLines and LineBreakMode in action on Android
In the above screenshot you can see the same functionality on Android and see that we get the ellipsis at the end when using it with LineBreakMode.TailTruncation.
Automatic Mode #
Wherever possible you can also set it to automatic to let the Label grow as your text needs. For instance, on iOS, Mac OS, and UWP you can do this by setting the MaxLines property to 0. The Label will now just grow as your text grows
That is all there is to it! Have a look at some other features I have implemented into Xamarin.Forms framework and of course, consider contributing yourself, it is pretty awesome!
This feature just got merged at the time of writing, so don’t expect it to be in there this minute. But I’m sure it will be part of a upcoming release. In the meantime you could check out the pre-release packages or nightly builds. Also, keep your eye on the release notes to see this pop up.
If you have anything you’d like to discuss regarding contributing or this specific feature, please don’t hesitate to reach out!