Greg
Greg

Reputation: 34818

iphone code for auto uiLabel & uiTableViewCell resizing when font changed?

Can someone summarise the key pieces of objective-c code that would assist in solving this.

Objective - Autoresize a UITableView after a user changes the font size. Therefore if the user increases or descreases font both the (a) uiLabel heights should change to ensure they nicely include text, and (b) uiTableViewCell heights should also adjust.

Assumption is that:

  1. UITableView has been extended via creation of a custom cell view - i.e. create a new view in a NIB that forms for the content view of the UITableView cells
  2. each custom cell has four UILabel's which form a 2 x 2 patter of UILabels - i.e. two on top line and two on bottom line
  3. so as the user increases the font the horizontal spacing of the UILabels would remain the same, however the label heights would change

So I assume the challenge / questions I have would include (and hopefully be answered by some sample code that someone can post)

  1. Is it OK that the starting point here is an already laid out custom UITableViewCell in InterfaceBuilder? (or does the solution require it is constructed totally programmatically)?
  2. How to calculate the heights the label's should be? Is this just with the NSString method "sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:", and do I need to add addition for margins etc?
  3. How programmatically to increase the height of the labels dynamically - in which method do you do this & how to change the height itself
  4. How to ensure that when UILabel at the top is expanded & grows, that it automatically pushes down the UILabel on the 2nd row? Does this happen by default or is there a specific property/setting you have to you to ensure this hapens.
  5. How to then automatically increase the height of the TableViewcell, after the above has occurred. Which method to do this in, and how programmatically to perform the increase & redraw.

Hope this makes sense.
thanks

Upvotes: 0

Views: 1881

Answers (2)

Ternary
Ternary

Reputation: 2421

To get the UILabel's height to match your target font, you use the sizeWithFont function of NSString.

NSString *myText = @"Go Hokies";
UIFont *myFont = [UIFont boldSystemFontOfSize:15];
CGFloat lineHeight = [myText sizeWithFont:myFont].height;

The problem you may run into is if the text doesn't fit horizontally in the bounds you've defined. If you want the font to downsize accordingly, turn on the adjustment flag and set a min like this.

myUILabel.minimumFontSize = 10;
myUILabel.adjustsFontSizeToFitWidth = YES;

Upvotes: 2

Nevin
Nevin

Reputation: 7819

  1. You can base on the XIB file as a default layout, then adjust the position/size later during runtime.
  2. Yes use that method to calculate the required height. You need to add margins between labels yourself.
  3. Change the label's frame.
  4. You need to calculate the x,y of the 2nd row's label base on the x,y,height from the 1st row's label + margin.
  5. hook with the following method and return the new height:
    • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 2

Related Questions