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:
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… 😉
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.
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.
Wherever possible you can also set it to automatic to let the Label grow as your text needs. For instance, on iOS, Mac OS
That is all there is to it! Have a look at some other features I have implemented into Xamarin
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!
4 thoughts on “MaxLines on Labels in Xamarin.Forms”
Excellent. Where do you find the time???
Beats me… There are now 48 hours in a day, right? 🤔
You should use iOS and Android screenshots with the same value for MaxLines. Otherwise one could get the impression, that you get 3 lines on Android, when you set MaxLines=”2″, but only 2 on iOS. 😉
Part of the feature! 😛 No, you’re right. Let me fix that. Thanks!
Comments are closed.