简体   繁体   中英

How can I see “rowcount” (?) of a textlabel in xamarin forms?

I have a label in my XAML that I bind to my data and sometimes the text is longer than others and if the text is long enough to get into 2 rows, i want to adjust my code a little bit to make the UI look better.

Is there a way to solve that in xamarin forms?

XAML

<Label x:Name = "title" />

CODE

title.Text = Title; //title is a string cointaining the text.

So do something with:

if(title.text > 2 (rows?))
{
//change the ui.
}

Or if there is a solution that sees if a label doesnt fit on a the row. so if i have a absolutelayout that only allows 1 row and the label recieves the "..." because it all cannot fit. can you do something with that? if (title.Text.Contains == "...") { // change size of label }

No you can't do that like that, as xamarin forms uses "plaftorm renderers", specific to each platform, to render your string.

In fact you have no way to know if your label will split on more than one line. What you can do is count the number of chars that can fit on one line on most devices, and customize your rendering based on this value.

If you still want to compute the number of lines, you can do it using specific platform code that needs the width in point of your label. For example on iOS you will use NSString GetSizeUsingAttributes like described here: https://forums.xamarin.com/discussion/10016/measuring-string-width-getsizeusingattributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM