UILabel Help

Discussion in 'iOS Programming' started by lukin, Sep 18, 2012.

  1. lukin macrumors regular

    Jul 24, 2008
    Hello, I am new to iOS development/XCode/Interface Builder etc and could use some help.

    I'm basically filling out a simple view. The view is just tall enough to hold some labels: 'Name' '[Name Value]' 'City' '[City Value]' etc.

    I'm struggling to get them to size appropriately. I've built the view in a xib, with the Labels going across the view.

    What happens is when I use the simulator, the sizes of the labels don't adjust to fit the name/city that are set programmatically. So if in IB the name value label is set to this big: [...................] and the name is short, it doesn't shrink the label like [Name], instead it looks like [Name..............]. So rather than being a fluid bar with name/value labels, it has spaces and looks bad depending on the length of the name.

    A shorter name might look like this:
    'Name Costanza............City New York'

    A longer name might look ok because it just gets close to the set length:
    'Name George Costanza City New York'

    When the name is really long, it overlaps Costanza and City.

    I cannot for the life of me figure out how to have these labels size to fit their text. Is there an easy way to do this?

    Thanks for guidance.
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You can try [label sizeToFit]; That will adjust the width of the label to the text in the label. If you want to move a second label based on the size of the first label you can also do that. Also, lookup sizeThatFits.
  3. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Why not use two labels?

    In the first label, have name and name value; in the second have city and city value?
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I think he is using two labels side by side. But maybe I'm not really sure. Maybe we need some screen shots.

Share This Page