0

I have a UILabel inside a UIScrollView and the contentSize of the UIScrollView is determined by the amount of text in the UILabel. I have managed to define a minimum height for the contentView inside the UIScrollView by making the height, width and aspect ratio constraints optional.

This is how the hierarchy of the UIScrollView looks like -

UIScrollView
    contentView
        UILabel

In some cases, even when the text is huge and makes the label expand beyond it's frame, I wish to have the frame of the UILabel stick to it's square frame with the extra text getting truncated. I am using autolayout so I believe this won't be possible by merely setting the frame of the UILabel and it has something to do with changing the priorities of the constraints.

Constraints for the UILabel - The UILabel has its leading, trailing, top and bottom connected to the contentView.

Constraints for the UISCrollView-

Required

Trailing, Leading, Top and Bottom to the superview i.e. the UIScrollView

Optional

Equal height and width to the superview (UIScrollView) and aspect ratio = 1:1

EDIT

And I need to do it in a way that when I set the lineBreakMode of the UILabel to NSLineBreakByTruncatingTail, the text automatically gets truncated and textLabel.text only returns the visible text.

genaks
  • 757
  • 2
  • 10
  • 24
  • why dont you use UITextView instead of UILabel? – Abdul Waheed Apr 25 '17 at 06:04
  • @AbdulWaheed I can't because of certain other limitations – genaks Apr 25 '17 at 06:05
  • use tableview instead of scrollview . – KKRocks Apr 25 '17 at 06:06
  • I would say that is more of a hack @KKRocks – genaks Apr 25 '17 at 06:12
  • Its hard to say, could you list all your constraints? Also, have you tried adding an inequality constraint for the label, such that `label.height <= label.width`? – Losiowaty Apr 25 '17 at 06:13
  • I get what you are saying though. Use UITableViewAutomaticDimension when I have to let it expand and use the width of label as it's height when I have to make it square-sized, right? – genaks Apr 25 '17 at 06:14
  • try this : http://stackoverflow.com/a/29365674/3901620 – KKRocks Apr 25 '17 at 06:15
  • no you need to add only top, bottom, trailing and leading constraints to label . and code of UITableViewAutomaticDimension. – KKRocks Apr 25 '17 at 06:17
  • @Losiowaty Wouldn't that inequality constraint make the label never expand beyond it's square frame? I need it to expand as long as it can but, let's say on the press of a button, I need to shrink it to a square with the extra text getting truncated – genaks Apr 25 '17 at 06:20
  • 1
    Ah, didn't catch that from the question - if you have an `IBOutlet` for a constraint you can simply disable/enable it whenever necessary, or even swap constraints. – Losiowaty Apr 25 '17 at 06:22
  • @Losiowaty I am adding all the constraints to my question. Can you please let me know which all constraints I need to swap? – genaks Apr 25 '17 at 06:23
  • Use this answer from Hashem Aboonajmi - http://stackoverflow.com/a/14633050/4533932 – genaks Apr 26 '17 at 04:45

1 Answers1

0

Ok, so if I understand everything correctly, you want to have an expandable label that either shows its entire text, or has a square form. This should as easy as enabling/disabling a constraint such that label.height <= label.width (or ==, depending on what you want it to look like if there is less text then would fit a square label). You can create this constraint in interface builder, add an outlet for it and then simply do:
self.labelSquareConstraint.active = YES/NO;

As to the question in your edit : there is no easy way to get the visible part of the text from a label, but there are some threads on SO about this, for example :
Get truncated text from UILabel
UILabel visible part of text
Calculate the range of visible text in UILabel

Community
  • 1
  • 1
Losiowaty
  • 7,911
  • 2
  • 32
  • 47
  • I have been using the code from that third link you provided. Unfortunately, it returns too big a substring to fit into the `UILabel` – genaks Apr 25 '17 at 06:46
  • I see 2 different approaches to this - 1. Calculate the amount of text that would fit into a square frame and set the text or 2. Have the label stick to its square frame somehow and then still calculate the visible text which renders the second approach useless altogether – genaks Apr 25 '17 at 06:48
  • The second answer from the third link did it for me. Even though a little inaccurate for some fonts, it does a good enough job – genaks Apr 26 '17 at 04:44