PDA

View Full Version : UILabel Help




lukin
Sep 18, 2012, 05:09 PM
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.



PhoneyDeveloper
Sep 18, 2012, 07:25 PM
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.

ArtOfWarfare
Sep 18, 2012, 07:27 PM
Why not use two labels?

In the first label, have name and name value; in the second have city and city value?

PhoneyDeveloper
Sep 18, 2012, 07:36 PM
I think he is using two labels side by side. But maybe I'm not really sure. Maybe we need some screen shots.