Category Archives: ios6

Dynamic height for UILabel to fit to content length

Starting with objective-c for iOS is a big step, when you come from the always dynamic “web”-world with HTML and CSS. Just put content in some HTML Tag and it normally grows with the length of it.

With objective-c and iOS a lot things are just different, like the elements you can choose to put content in, or the way navigation works.
One common problem – especially for beginners – is to accept that you have to calculate sizes for elements programmatically, e.g. UILabel.
When we use the interface builder, there are controls for that, but this is all static so far, e.g. “Lines”.

So how can i resize the UILabel if i do not know the content length?
It is not as complicated as it seems in the first place.

Bildschirmfoto 2013-05-01 um 11.16.47

If you use the Interface Builder and put a UILabel in your layout it is important to set “Lines” to 0. i often forgot this, so i think its guess is a common mistake, this is why i point it out 😉

Now we fill the UILabel (here: publicTransport) with a value from a NSDictionary and resize it to the necessary size.

publicTransport.text = [addresslist valueForKey:@"public"];
NSString *message = [addresslist valueForKey:@"public"]; 
CGSize maximumLabelSize = CGSizeMake(280,1000);

// use font information from the UILabel to calculate the size
CGSize expectedLabelSize = [message sizeWithFont:publicTransport.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

// create a frame that is filled with the UILabel frame data
CGRect newFrame = publicTransport.frame;

// resizing the frame to calculated size
newFrame.size.height = expectedLabelSize.height;

// put calculated frame into UILabel frame
publicTransport.frame = newFrame;

Just copy this code and paste it into your project. Replace the label name publicTransport with the label name you need to resize and this should all work fine 😉


Create dynamic height for UITableViewCells in iOS

During my try creating a forum view with threads and posts for ios devices i figured out that there is no option to get an automatic height for table cells if the content is larger then the layout i created in the .xib file.

After not finding a proper and easy solution in several developer forum, it took me some days to figure out a way to get it done in a way that seems to work for me.
Calculating the size of the content seems to do the trick.

In my layout i have label for the username, a label for the date of the post and the message itself.
The message label is the only content, that could cause any issues, so this is the only part that needs to be calculated.

Here is the code that does it for me:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSObject *Thread = [_postArray objectAtIndex:indexPath.row];
    NSString *message =[Thread valueForKeyPath:@"Forum.message"];
    CGSize maximumLabelSize = CGSizeMake(296,9999);
    UIFont *font = [UIFont systemFontOfSize:17];
    CGSize expectedLabelSize = [message sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
    int a = expectedLabelSize.height + 55;
    return a;

– you can get systemFontSize also through a function, so it could be dynamic
– 55 is the fix height of the content above the message label

If there are other – maybe better or more comfortable – ways, please let me know. 🙂