Forum Xamarin Xamarin.Forms

Know or Find if text gets wrapped in Label

Hi,

Is there any possibility to know / find if text in label gets wrapped or its being appeared in multiline.

For e.g on large screen with normal font size text inside label appears without wrapped i.e. in single line.
For small screen or with large font size (user device setting), text gets wrapped. (Already checked with LineBreakMode option, that not fulfilling the requirment)

So only in case when text gets wrapped, needs to change font size smaller or other action like change label text itself etc.

Thanks

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Is there any possibility to know / find if text in label gets wrapped or its being appeared in multiline.

    Get the width_value of Label.Text and current screen's width. Compare the two values, and you'll get if the text of Label has been wrapped or appeared in multi lines.

    page.xaml.cs

    //get the Text.Length of label
    var length = label.Text.Length;
    //get the Label.FontSize of label
    var size = label.FontSize;
    //the value of label.Text's width 
    var textWidth_value = size * length;
    
    //the width of current screen
    var current_screen = Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Width;
    
    if (textWidth_value > current_screen)
    {
        //label is wrapped or has multi lines
    }
    else
    {
    }
    

    page.xaml

    <Label x:Name="label" FontSize="10" LineBreakMode="WordWrap" Text="text" />
    
  • JohnHardmanJohnHardman GBUniversity admin

    @Jarvan

    (1) That suggests that FontSize is related to the width of a character, as opposed to the height. Is that the case? Documentation just refers to "size" rather than "width".
    (2) Does that take into account scaling as a result of accessibility options?

  • toughcannytoughcanny Member ✭✭

    Thank you Jarvan but it didn't work out in required case. I think your solution may work for one Label per line span on screen width.

    Where as the required label itself is within a Grid Column (of fixed width definition) with other columns.
    So workaround appears to use SizeChanged event of label.

Sign In or Register to comment.