0

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?

Padin215
  • 7,444
  • 13
  • 65
  • 103

2 Answers2

1

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).

Community
  • 1
  • 1
Firo
  • 15,448
  • 3
  • 54
  • 74
0

Presumably you've got any array of names. What you'll do is enumerate the list of names, determine the lengths required for each one, and then store the greatest one and apply it as the desired width. You will need to define a "maximum size" that should not be exceeded. A method along these lines should return something suitable:

- (CGFloat)suitableWidthForLabel:(UILabel *)nameLabel withNames:(NSArray *)arrayOfNames
{

    CGFloat suitableWidth = 100.0f; // A reasonable starting point
    CGSize maximumSize = CGSizeMake(600.0f, nameLabel.frame.size.height);
    UIFont *labelFont = [nameLabel font];

    for (NSString *aName in arrayOfNames)
    {

        CGSize expectedLabelSize = [aName sizeWithFont:labelFont
                                        constrainedToSize:maximumSize
                                            lineBreakMode:nameLabel.lineBreakMode];

        suitableWidth = MAX(suitableWidth, expectedLabelSize.width);
    }

    return suitableWidth; // This will be the largest size you may need to accommodate
}
isaac
  • 4,867
  • 1
  • 21
  • 31