I m new to objective-C..
I want to set dynamic size(width
and height
) of UILabels
.I have a UILabel
with some text in it.If the text is large it should display correctly if it doesnot fit in that one line by going to other line and if the data completes then only next UILabels
data should start and same concept for the next UILabel
also.How can I get it...?
Asked
Active
Viewed 2,683 times
0

Matthias Bauch
- 89,811
- 20
- 225
- 247

Honey
- 2,840
- 11
- 37
- 71
-
check my answer given here http://stackoverflow.com/a/12600584/1538079 – Niru Mukund Shah Nov 02 '12 at 06:46
-
possible duplicate of [How to calculate UILabel height dynamically?](http://stackoverflow.com/questions/7174007/how-to-calculate-uilabel-height-dynamically) – Matthias Bauch Nov 02 '12 at 07:37
2 Answers
1
Following is the code for the viewcontroller.h file
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UITableView *dataTableView;
NSMutableArray *items;
}
@end
Following is the code for the viewcontroller.m file
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
items = [[NSMutableArray alloc] init];
[items addObject:@"Happiness is having a large, loving, caring, close-knit family in another city.\n\n\t\t-George Burns (1896 - 1996)"];
[items addObject:@"When I am abroad, I always make it a rule never to criticize or attack the government of my own country. I make up for lost time when I come home.\n\n\t\t-Sir Winston Churchill (1874 - 1965)"];
[items addObject:@"After two years in Washington, I often long for the realism and sincerity of Hollywood.\n\n\t\t-Fred Thompson, Speech before the Commonwealth Club of California"];
[items addObject:@"It is a profitable thing, if one is wise, to seem foolish.\n\n\t\t-Aeschylus (525 BC - 456 BC)"];
[items addObject:@"Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.\n\n\t\t-Dave Barry"];
[items addObject:@"At the worst, a house unkept cannot be so distressing as a life unlived.\n\n\t\t-Dame Rose Macaulay (1881 - 1958)"];
[items addObject:@"It is curious that physical courage should be so common in the world and moral courage so rare.\n\n\t\t-Mark Twain (1835 - 1910)"];
[items addObject:@"The knowledge of the world is only to be acquired in the world, and not in a closet.\n\n\t\t-Lord Chesterfield (1694 - 1773), Letters to His Son, 1746, published 1774"];
[items addObject:@"What lies behind us and what lies before us are tiny matters compared to what lies within us.\n\n\t\t-Ralph Waldo Emerson (1803 - 1882), (attributed)"];
}
#pragma mark -
#pragma mark UITableView Delegaates
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text 1="systemFontOfSize:FONT_SIZE" language="sizeWithFont:[UIFont"][/text] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] ;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text 1="systemFontOfSize:FONT_SIZE" language="sizeWithFont:[UIFont"][/text] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[dataTableView deselectRowAtIndexPath:[dataTableView indexPathForSelectedRow] animated:YES];
}
@end
Also Download Source code from here.
May this helping a lot.

Nimit Parekh
- 16,776
- 8
- 50
- 72
0
How to set label according to text but i want background color also
use this link and here you use "textsize" to set your label frame and backview

Community
- 1
- 1

Waseem Shah
- 2,219
- 23
- 23