2

I have a UIPopoverController in my app which simply displays two UILabels beside each other with a list of words in each of them. However sometimes there are only a couple of words in each list meaning there is tons of blank space in the popover view.

How can I make it so that the popover view in at least height dynamically adapts to how many lines of words there are in my label?

1 Answer 1

2

If text in label is specified before popover shows, you can achieve this by using similar code in viewDidLoad method:

- (void)viewDidLoad { [super viewDidLoad]; // ... CGFloat height = [label.text sizeWithFont:label.font forWidth:label.frame.size.width lineBreakMode:label.lineBreakMode].height; // This calculates only height of the label, you may want to add some margins, etc. CGSize size = CGSizeMake(self.view.frame.size.width, height); self.contentSizeForViewInPopover = size; } 
Sign up to request clarification or add additional context in comments.

2 Comments

I can see how this would work, but sadly it isn't. Im getting a very narrow thin view. When I fill my labels with words, I add a word, then new line it with \n and again and again etc. When you pass the text value of the label, might it not detect the line breaks?
For what I'm aware, sizeWithFont:forWidth:lineBreakMode: doesn't respect newline characters. You should try instead sizeWithFont:constrainedToSize:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.