Search code examples
iosobjective-cuitableviewuipopovercontroller

Display UITableViewController in UIPopoverController with variable width


I will use a UIPopoverController to display a UITableViewController. The table view will show a list of names with unknown lenghts (could be Bob or could be Alexander).

I know how to vary the height of the popover, but i can't seem to figure how to vary the width of it so a name does not get truncated.

Need to figure out the width of a name so i can set the popover to that width so names don't bleed out of the popover:

enter image description here

any ideas?


Solution

  • Get the width of the string and then set the UITable to be that width. Here is a method you could add to get the width. You could add it to a category or to your class. I took the code from here.

    - (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font {
         NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
         return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width;
     }
    

    This way you can specify the font you are using in case you end up changing it in the future. (Different fonts obviously will result in different widths).