0

Say I have two labels, one on top of the other. Label 1 is set to have a maximum number of lines of 2 (numberOfLines=2). So sometimes, depending on the text, Label 1 can take up TWO lines. The thing though is that a maximum of 2 lines should be shown between both labels, where Label 1's second line takes precedence over Label 2. (Also, Label 2 is always 1 line.)

So either of these 2 scenarios are possible:

Label 1 text
Label 2 text

OR

Label 1 text (line 1)
Label 1 text (line 2)

The only way I thought to attack this was to simply hide Label 2 if Label 1 is taking up 2 lines. But the problem is, how can I determine how many lines Label 1 is taking up?

I've found a few other answers about this (1, 2), but none seem to work for me. Is there perhaps a better way to go about this?

AJP
  • 57
  • 4

2 Answers2

0

In my opinion, you should just use one label, and construct the text seperately by the code and pass the final text into the label. In order to have a new line, you can use "\n". If you want to have a different fonts/color etc, you can always use AttributedString. This will be much cleaner, easier to change, and more flexible to maintain in the future.

salanswift
  • 36
  • 3
  • this is a great answer thanks. i do however seem to have one issue with this approach. if the first part of the text (formerly known as `Label 1`) takes up 2 lines, i don't want to see the ellipsis ("...") showing (since the new line character and text of what was known as `Label 2` is now part of the same label), because i still consider them 2 different pieces of text. i WOULD want the "..." to show if the first part was say 3 lines and was being truncated to 2. is this possible? – AJP Jul 03 '21 at 19:21
  • Thanks for acknowledging. What I understood that you want to have elipsis on the second line only if the text overflows and not on the first line, I think you can easily achieve this if you keep the number of line upto 2, and the rest will be exactly what you have mentioned. – salanswift Jul 08 '21 at 07:13
0

Did you working with Constraint? or Did you have knowledge about NSLayoutConstraints?

you can easily do that with managing proper constraint and it doesn't matter how many line will text have you can show both label with text.

make sure label.numberofline = 0. // It will take line as text.

jatin fl
  • 157
  • 6