How to set a max length for label text in Xamarin - xamarin.android

In xamarin, I would like a Label Text with a max length. When the text is too long, it is on several lines. So, I would like set a maximum length or number of lines. I think to measure the string length but I no found method.
I hope there is a solution to Xamarin. I don't want to do specific code for each platform if it's possible.
Thank you

MaxLines property is your answer. It's bindable too.
And if you want two show three dots after certain lines for overflowing text , use LineBreakMode="TailTruncation"
Example:
<Label Text="{Binding LargeText}" MaxLines="2" LineBreakMode="TailTruncation"/>

Have you looked at LineBreakMode? https://developer.xamarin.com/api/type/Xamarin.Forms.LineBreakMode/
This is what I used to actually make my labels longer.

In Xamarin.Forms 3.4 you can use Label's "MaxLines" property to set maximum lines. Also, it's a bindable property

Related

How can I add the three dots of a UIlabel when the text is too long, at the middle of the text instead of at the end?

For example, I have the following two string: "How Munched is That Birdie in the Window?" and "S22 - E7". I want to present in the label the following: "How Munched is That Birdie in ... S22 - E7" If the string is too large according to the label's size and doesn't fit it". How you can see, the three dots are placed always in the first string, the second string is always shown full.
How can I achieve this?
Here is how to do it.
yourlabel.adjustsFontSizeToFitWidth = false
yourlabel.lineBreakMode = .byTruncatingMiddle
You can set the UILabel's ParagraphStyle LineBreakMode to byTruncatingMiddle, which will probably work in most cases with carefully planned label size. From the docs:
The line is displayed so that the beginning and end fit in the container and the missing text in the middle is indicated by an ellipsis glyph. This mode is used for single-line layout
If you want to guarantee none of the "S22 - E7" string is truncated, you would have more control using two labels and setting layout constraints such that the width of the protected label is preserved so that it can display the full string whenever possible, but that is probably overkill in most cases.
You may also find this answer helpful if decide to go a different route by manually manipulating the displayed string based on detecting how many characters will be visible given the width and font.

UILabel divides single word

Good day folks,
Actually I'm going crazy, I did everything I could in order to solve this simple problem.
As you see, a simple label in a narrow space causes the single word "Verification" to be separated into two lines which is not acceptable of course.
I know that I could make number of lines only 1 and this will decrease font size, I tried all wraps modules and all fails.
What can I do to display the label as "Verification Process" without any separation of a single word? (I accept even shrinking font size or clipping last word).
The first one is default setting
Does these two settings below meet your needs?
If you are ok with clipping of the words, then you can set the lines property of your label to 1. Or you want to reduce the font size that also can be done using storyboard property inspector. Hope this will help.
please try this.
my testLabel and number of lines equal to 2.
self.testLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.testLabel.text = #"verificaton process";

How to adjust Font Size according text viewable in a TButton.Text using Firemonkey?

I am using buttons to display product names in a matrix using TGridLayout.
The problem is that commonly Items contains 3 or 4 words and in my language (Portuguese) some words tend to be long.
I would like that somehow I could calculate the size of the font, decreasing it, in order to make all the text show up automatically (of course there is also a decrease limit, anything below 9 or 8 point for the font turn to be difficult to read).
The wordwrap property is turned on to have many lines, and use the most possible space for the text.
I don't know if you're programming for an Android/iOS app, but you can't change the fontsize of a button. I've had the same problem, my solution was to make an abbreviation of the words. And then i put labels above it to explain the abbreviations.
Of course you can adjust the font size of a button:
TButton.TextSettings.Font.Size

Is there a way to make long strings wrap in addition to autoshrink?

I have dynamic text in a label with plenty of room around it. Since there is room, it would be great if the text could wrap in addition to autoshrinking.
You can apply any of these 2 solutions
1.Set numberOfLines To 0 and make larger height of your label then text automatically wrap.
2.you can dynamically calculate height of label on the basis of length of string with function sizeWithFont.

How to draw a non-rectangle UILabel with paragraph truncation at the end?

I need to show a body of text similar to the one shown below and limited to the red non-rectangular area. Rob's answer to this question pretty much answers my question as well, but I also need to truncate at tail of the paragraph when the text is too long.
Extra question: is it also possible to set minimum font size similar to UILabel?
Try using NSString::sizeWithFont:constrainedToSize method which is documented here. You can specify the fontsize and maximum size for the text.
You can also calculate the minimum font size using NSString::sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: method.

Resources