When I set a UILabel as 0 number of rows, I understand that it causes the label to increase in number of rows based on the length of the text. Is there a way to set the maximim nuber of rows so that when the text is longer than the label allows, it shows an ellipses and limits the number of rows to the maximum set? For example, if I set the maximum number of rows to 3, then if the label needs 4 rows in order to show the entire text, it instead shows only 3 rows and shows an ellipses at the bottom right-hand side of the label to indicate that the text can't fit.
Accepted Reply
Have you bothered to read the documentation? To quote:
"
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is
1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode, as specified by the
lineBreakMode property.When the label is resized using the
sizeToFit() method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit() method resizes the receiver so that it is big enough to display three lines of text."
The default linebreak mode is to truncate the end and add ellipses. That's what you're asking for.
Replies
That's what setting a non-zero value does.
Yes. I understand that. Did you read the question? I would like to be able to set a maximum number of rows for the label. For example, if I set the maximum number of rows to 3, then if the label needs 4 rows in order to show the entire text, it instead shows only 3 rows and shows an ellipses at the bottom right-hand side of the label to indicate that the text can't fit.
Have you bothered to read the documentation? To quote:
"
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is
1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode, as specified by the
lineBreakMode property.When the label is resized using the
sizeToFit() method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit() method resizes the receiver so that it is big enough to display three lines of text."
The default linebreak mode is to truncate the end and add ellipses. That's what you're asking for.
I don't think you understand. I want the label to be able to expand in number of lines up to a certain number. For example, if I set the maximum number of lines to 3, then the label can have 1, 2 or 3 lines, depending on how many lines is needed to show the text. But if the text requires 4 lines, then the label would show only 3 lines and would show the ellipses (...) at the end to show that there is more text not showing. I want the best of both worlds at the same time. I want to be able to auto-increase the number of lines and at the same time limit the number of lines to a maximum number, in the case of our example, 3 would be the maximim number.
I don't know if this is possible, but it's what I would like.
You can achieve it by setting label maximum row properties and adding width and height constraints to the label.
That does work. However, if at all possible, I would like to add the ellipses (...) at the end of the visible text to indicate that there is more text. Is there any way of checking where the text stops showing, or which part of the text is showing and which part doesn't show?