Pages

Sunday 12 January 2014

iPhone: Dynamic Resizable UILabel and UITableViewCell


Dear friends, I am very glad to post this simple concept of uilabel resizing depending on text size with uitableview. This is my first post on blog. I believe, uilabel resizing with uitableview will be helpful to many beginners.

Kindly follow steps involved for uilabel resizing with uitableview.

Step 1: Create project by selecting Single View Application with story board from Xcode.

Step 2: Place UITableViewController on story board from object library.

Step 3:Select UITableViewCell on UITableViewController.

Step 4:Change UITableViewCell Style->Custom, set UITableViewCell Identifier->Cell

Step 5:Place UILableView On UITableViewCell.

Step 6: Create New File Name the Class as "SampleCell" and choose SubClass "UITableViewCell" in the Attributes Inspector.

Step 7: Copy and Paste bellow codes to SampleCell.h and SampleCell.m files.

SampleCell.h

#import <UIKit/UIKit.h>

@interface SampleCell : UITableViewCell
{
    IBOutlet UILabel *testLabel;
}

@property(retain,nonatomic)UILabel *testLabel;

@end

SampleCell.m


#import "SampleCell.h"

@implementation SampleCell

@synthesize testLabel;


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

Step 8: Now change the UITableViewCell class to "SampleCell" in the Identity Inspector.
Step 9: Link the UILabel with "testLabel" by referring outlet in the Connection Inspector.

Step 10: Create New File Name the Class as "SampleTable" and choose SubClass "UITableViewController" in the Attributes Inspector.
Step 11: Copy and Paste bellow codes to SampleTable.h and SampleTable.m files.

SampleTable.h


#import <UIKit/UIKit.h>

@interface SampleTable : UITableViewController

@end


SampleTable.m

#import "SampleTable.h"
#import "SampleCell.h"

@interface SampleTable ()

@end

@implementation SampleTable

int a=0;
NSString *test=@"My question essentially boils down to the best way to support dynamic heights of UILabel's (and I suppose other elements) in a UITableCell, and also correctly resize the label width/height and cell heights when rotating. ";

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   
    // This is used to notify the rotation of the device.

  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *CellIdentifier = @"Cell";
    SampleCell *cell =(SampleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.testLabel.text=test;
    if ([self isPortrait]) {
       
        cell.testLabel.frame=CGRectMake(10,0,cell.frame.size.width,cell.frame.size.height);
    }
    else
   
   cell.testLabel.frame=CGRectMake(10,0,[[UIScreen mainScreen] bounds].size.height
10,cell.frame.size.height);
 
   cell.testLabel.numberOfLines=0;
   [cell.testLabel sizeToFit];

   return cell;
}
- (void) didRotate:(NSNotification *)notification
{
    [self.tableView reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
   
    SampleCell *cell = (SampleCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    //Set the maximum size
    CGSize maximumLabelSize = cell.testLabel.frame.size;
    //Calculate the new size based on the text
    CGSize expectedLabelSize = [test sizeWithFont:cell.testLabel.font constrainedToSize:maximumLabelSize lineBreakMode:cell.testLabel.lineBreakMode];

    return expectedLabelSize.height;
}

-(BOOL)isPortrait
{
    UIInterfaceOrientation orientation=[[UIApplication sharedApplication] statusBarOrientation];
   
    if(orientation==UIInterfaceOrientationPortrait ||
orientation==UIInterfaceOrientationPortraitUpsideDown)return YES;
    return NO;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end

Step 12: Now change the UITableViewController class to "SampleTable" in the Identity Inspector.

Step 13: Run and Enjoy.

No comments:

Post a Comment