views:

310

answers:

3

Hi,

I currently have a custom UITableViewCell which contains a few labels, and an image. The "main" label is used to display people's names. Currently, I'm styling it in bold text. What I'd like to do (to gain some space and readability), is to mimic the Address Book app cell style, that is: first name in light text, and family name in bold text. Is there a way to do this using the same UILabel? Or, should I use 2 different UILabels? How should I layout them, without knowing their sizes?

Thanks in advance for your assistance!

A: 

You can use the built-in UITableViewCellStyleValue2. From the UITableViewCell.h header file:

UITableViewCellStyleValue2,     // Right aligned label on left with blue 
                //text and left aligned label on right (Used in Phone/Contacts)

Pass this into your [[UITableViewCell alloc] initWithStyle:...] method.

Ben Gottlieb
In my case, using the default UITableViewCell styles is not possible (I'm using 4 UILabels and a UIImage already).Thanks anyway!
Romain
You can always add additional labels after the default ones are laid out.
Ben Gottlieb
A: 

I think it's table view cell with UITableViewCellStyleDefault style. If you'd like to use different font attributes then you need to use different labels (because there is no attributed strings yet). To calculate size of these labels you should use following method:

- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode;

or any relevant from NSString(UIStringDrawing).

Also, you can use custom strings drawing instead of UILabel.

Roman Busyghin
Thanks very much, the basic version of (CGSize)sizeWithFont:(UIFont *)font made the trick!In less than 5 mins I managed to get what I wanted.Again, thanks :-)
Romain
A: 

See this sample code from atebits:

http://atebits.cachefly.net/blog/FastScrolling/FastScrolling.zip

It does something similar to what you want.

macatomy
Thanks, this example indeed does what I'm looking for.I'll stick to the sizeWithFont solution cause it also works great, but I'll keep in mind this one if I ever need it one day.
Romain