Xamarin Forms, Label with proprety LineBreakMode="MiddleTruncation" can't be shown in 4 line

imad diraa 41 Reputation points
2021-09-09T15:42:36.98+00:00

Hello Everyone,
I'm trying to use the middletruncation attribute in more than one line, but xamarin dosen't allowa me to do that.

is there anyway to do that?

this is my code:

<?xml version="1.0" encoding="utf-8" ?>  
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
             x:Class="App9.MainPage">  
    <StackLayout>  
        <Label FontSize="26" Padding="30,24,30,0" LineBreakMode="MiddleTruncation" MaxLines="4">  
            <Label.FormattedText>  
                <FormattedString>  
                    <FormattedString.Spans>  
                        <Span Text="first span "/>  
                        <Span FontSize="26" FontAttributes="Bold" Text="a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big   
                              line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line  
                              a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line   
                              a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line a big line "/>  
                        <Span Text="third span"/>  
                    </FormattedString.Spans>  
                </FormattedString>  
            </Label.FormattedText>  
        </Label>  
    </StackLayout>  
</ContentPage>  
  

this is the result:

130779-middletruncation-result.png

thanks to everyone

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-09-10T03:03:56.453+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I'm trying to use the middletruncation attribute in more than one line, but xamarin dosen't allowa me to do that.

    We cannot do that. LineBreakMode.MiddleTruncation truncates the middle of text and replaces it with an ellipsis, this function requires to make sure the 'label' is single line. From the result, we can see that set a value to the MaxLine doesn't work.

    Xamarin.Forms is an open-source UI framework. Xamarin.Forms allows developers to build Xamarin.Android, Xamarin.iOS, and Windows applications from a single shared codebase. Xamarin.Android or Xamarin.iOS is only a layer of wrapper for Android/iOS native libraries, no extra operations. In native Android, we use setEllipsize command to set the line break mode and the middle mode is bases on setting setSingleLine to true. Here is the related doc, you could refer to: https://developer.android.com/reference/android/widget/TextView#setEllipsize(android.text.TextUtils.TruncateAt)

    In native iOS, NSLineBreakMode.byTruncatingMiddle also uses this mode for single-line layout. Here is the documentation: https://developer.apple.com/documentation/uikit/nslinebreakmode/bytruncatingmiddle

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful