I want to resize the font of my UITableView cells.
If they contain too long titles, the textLabel is split.
So, how can i resize the font when the label length goes over 20 character, in example?
I thought:
NSString *cellText = cell.textLabel.text;
if (cellText.length > 20){
cellText = [UIFont systemFontOfSize:11.0];
}
But there's something wrong 'cause it crashes.
Any ideas?
cellText is an NSString and you are setting a UIFont to NSString pointer, you should set Font to textLabel, like this:
NSString *cellText = cell.textLabel.text;
if (cellText.length > 20){
cell.textLabel.font = [UIFont systemFontOfSize:11.0];
}
if not the problem then, please post the crash log also.
You don't need code for this !
in the inspector :
Set font size
set a min font size
And then check the "ajust to fit"
NSString *cellText = cell.textLabel.text;
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 300, 20)];
cellLabel.text =cellText;
cellLabel.adjustsFontSizeToFitWidth = NO;
cellLabel.numberOfLines = 0;
[cellLabel setBackgroundColor:[UIColor clearColor]];
[cellLabel setFont:[UIFont fontWithName:#"Arial" size:14.0f]];
cellLabel.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:cellLabel];
[cellLabel release];
Related
I have a project from a year ago that worked.
I have coded:
cell.textLabel.numberOfLines = 0; // Multiline
to enable multiline text in UITableView cell, and it worked before on iOS 5 (not tested it since beginning of 2012), now the multiline doesn't work, and shows only 2 rows of text in cell.
Did I miss something in iOS 6? Was there some kind of change or bug that causes this?
EDIT: I've tried cell.textLabel.numberOfLines = 5; for testing purposes, and it shows 2 rows again
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
Write inside cellForRowAtIndexPath
UILabel *lbl = [[UILabel alloc]init];
lbl.text = #"Your text value";
lbl.numberOfLines = 0;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
//Set frame according to string
CGSize size = [lbl.text sizeWithFont:lbl.font
constrainedToSize:CGSizeMake(300, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
[lbl setFrame:CGRectMake(0 , 0 , size.width , size.height)];
[cell.contentView addSubview:lbl];
Your heightForRowAtIndexPath method should be like this
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *value = [listOfData objectAtIndex:indexPath.row];
CGSize boundingSize = CGSizeMake(300, CGFLOAT_MAX);
CGSize stringSize = [value sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
return ((stringSize.height>44.00)?stringSize.height:44.00);
}
Note: Things you need to change
Here 300 is width of label . You can place your width here
Your can also change X and Y if it is dynamically added.
UILineBreakModeWordWrap is deprecated in iOS 6. Use NSLineBreakByWordWrapping
If you want to display you cell text with multiple then you can also put UILabel on cell.contentView, and give labelName.numberOfLines = 0 for display text tin multi line.
For EX:
UILabel *lbltitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 270, 35)];
lbltitle.backgroundColor = [UIColor clearColor];
NSString *originalString = #" Indian version of this popular search engine. Search the whole web or only webpages from India. Interfaces offered in English, Hindi, Bengali, Telugu, Marathi";
lbltitle.text= originalString;
lbltitle.font =[UIFont fontWithName:#"Arial-BoldMT" size:16];
lbltitle.textAlignment = UITextAlignmentLeft;
lbltitle.numberOfLines = 0;
lbltitle.textColor=[UIColor blackColor];
[cell.contentView addSubview:lbltitle];
Above code is put on cellForRowAtIndexPath method in between
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Put here Label Code
}
Alos read this How do I create a multiline table cell in iOS?
All,
I am very new to iphone programming. In the following code, I want the text to show all of the text within the comment label but right now it is truncating it. numberofLines is not working right either. Right now it is doing this. "My name is Fred and I aint dead..." but I want it to display the full text "My name is Fred and I aint dead yet so let me live" even if it has to be on multiple lines.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [self.allCells objectForKey:[NSNumber numberWithInt:indexPath.row]];
if(!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"UserCell2" owner:nil options:nil] lastObject];
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self.allCells setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]];
}
GSAsynImageView *imgView = (GSAsynImageView*)[cell viewWithTag:1000];
UILabel *lblTitle = (UILabel*)[cell viewWithTag:1001];
UILabel *lblComment = (UILabel*)[cell viewWithTag:1003];
UILabel *lbltime = (UILabel*)[cell viewWithTag:1004];
//lblComment setLineBreakMode:NSLineBreakByWordWrapping];
//lblComment.numberOfLines = 0;
//lblComment.lineBreakMode = UILineBreakModeCharacterWrap;
if(self.arrComments.count==0)
{
imgView.hidden = YES;
lblTitle.text = nil;
lblComment.text = nil;
lbltime.text = nil;
if(indexPath.row==1)lblTitle.text = #"No comments yet";
}
else
{
imgView.hidden = NO;
NSDictionary *dcUser = [self.arrComments objectAtIndex:indexPath.row];
NSString *strBio = [dcUser objectForKey:#"CommentTxt"];
NSString *strDisplayName = [dcUser objectForKey:#"CommenterDisplayName"];
NSString *imgName = [dcUser objectForKey:#"ImageName"];
NSString *usernamex = [dcUser objectForKey:#"CommenterUserName"];
if([imgName isKindOfClass:[NSString class]])
{
if([imgName rangeOfString:#"facebook"].location!=NSNotFound || [imgName rangeOfString:#"twimg"].location!=NSNotFound)
[imgView loadImageFromPath:imgName];
else
[imgView loadImageFromPath:[NSString stringWithFormat:#"%#images/%c/%#/50x50%#",WEBSERVER,[usernamex characterAtIndex:0],usernamex,imgName]];
}
lblTitle.text = strDisplayName;
lblComment.text = strBio;
lbltime.text = [self getDateTitle:[dcUser objectForKey:#"Date"]];
}
return cell;
}
try this bellow code and add in your cell..
UILabel * lblTitle = [[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(110, 31, 200, 50)];
lblTitle.text = #"your Text ";
lblTitle.lineBreakMode = UILineBreakModeWordWrap;// add this line
lblTitle.numberOfLines = 0;// add this line
lblTitle.font = [UIFont fontWithName:#"Helvetica" size:12];
for more detail see my blog with this post from THIS link
try this...
UILabel * label = [[UILabel alloc]init];
[label setFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
label.text = #" Text to be displayed in label ";
label.lineBreakMode = UILineBreakModeWordWrap ;// Wrap at word boundaries
label.numberOfLines = 0;// this line include multiple lines
[cell addSubview:label];
I think it probably because the size of the string you wanna show on the label exceeds the size of that label
You can use the properties of UILabel to increase the number of lines.
Like UILabel* para = [[UILabel alloc]init];
para.numberoOfLines = 10;
and also try changing para.lineBreakMode
Increase the height of the label and make the number of line to 2 for the particular label.
try this code,
lblComment.numberofLines = 2;
[lblComment setFrame:CGRectMake(100,20,200,70)];
The easiest way to do this is in a storyboard or xib. You can add your label (and anything else you want) to the custom cell you get automatically when you have a table view. Make sure your label has a specific width set, and that it has constraints to the top and bottom of the cell (make sure you set number of lines to 0). If you have these, the label will expand with the height of the cell, which you set in tableView:heightForRowAtIndexPath:.
I have to place two UILabels(state,zipCode) side by side i.e California 32320 in UITableView's cell.I can see them clearly when the fontsize is less.When I increase the font-size I couldnot see the last letter of state label and zipCode label is getting added.This is the code I m working on:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
state=[[UILabel alloc] initWithFrame:CGRectZero];
state.tag = 116;
state.backgroundColor = [UIColor clearColor];
[state setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[state setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:state];
zipCode=[[UILabel alloc] initWithFrame:CGRectZero];
zipCode.tag = 121;
zipCode.backgroundColor = [UIColor clearColor];
[zipCode setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
[zipCode setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:zipCode];
}
NSString *state1=[d objectForKey:#"State"];
CGSize constraint6=CGSizeMake(175,2000.0f);
CGSize size6=[state1 sizeWithFont:[UIFont fontWithName:#"Helvetica" size:12] constrainedToSize:constraint6 lineBreakMode:UILineBreakModeWordWrap];
state=(UILabel *)[cell viewWithTag:116];
state.frame=CGRectMake(105, city.frame.size.height+city.frame.origin.y+5, size6.width, 10);
[state setTextAlignment:UITextAlignmentLeft];
[state setText:[d valueForKey:#"State"]];
NSString *zip = [d objectForKey:#"Zip"];
if([zip isEqualToString:#""])
{
zipCode=[[UILabel alloc]initWithFrame:CGRectMake(105, 125, 320,10)];
zipCode .font=[UIFont fontWithName:#"Helvetica" size:12];
[zipCode setTextAlignment:UITextAlignmentLeft];
zipCode.hidden=YES;
[zipCode release];
}
else
{
NSString *zip=[d objectForKey:#"Zip"];
CGSize constraint200=CGSizeMake(175,2000.0f);
CGSize size200=[zip sizeWithFont:[UIFont fontWithName:#"Helvetica" size:12] constrainedToSize:constraint200 lineBreakMode:UILineBreakModeWordWrap];
zipCode=(UILabel *)[cell viewWithTag:121];
zipCode.frame=CGRectMake(13+state.frame.size.width+state.frame.origin.x, city.frame.size.height+city.frame.origin.y+5, size200.width,10);
[zipCode setTextAlignment:UITextAlignmentLeft];
[zipCode setText:[d valueForKey:#"Zip"]];
zipCode.numberOfLines=0;
}
return cell;
}
Now I can see the result as
Californi 32320
But when I reduce the font-size to 10 then I can clearly see them (California 32320).But I need the font-size to be 12 only as per my requirement.
Where I m going wrong?
Use this code it will saw the label in fit to size of your label
label.adjustsFontSizeToFitWidth = YES;
Please update the Label width with some what large. And use the "setNoOfLine" property of Lable to 2. So that, it will display text in 2 lines.
Hope it will be helpful to you.
Cheers!
There is a little trick by which the label will auto resize according to your text
UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,200,40)];//set your frame
testLabel.numberOfLines = 0;//Important to add this line
testLabel.font = [UIFont boldSystemFontOfSize:15];//Set your font
testLabel.text = #"Set your text here!";
[testLabel sizeToFit]; // this line will do the trick :)
The font of your state label is Helvetica-Bold, but in your sizeWithFont, the font you specified is Helvetica. Alternatively, you can just specify state.font.
Do like this,
CGFloat lLabelWidth = [yourText sizeWithFont: factLabel.font].width;
CGRect _tempFrame=yourLabel.frame;
_tempFrame.size.width=lLabelWidth;
yourLabel.frame=_tempFrame;
hope it will helps you....
I'm very much new to iphone development and strucked at a point. Actually my need is I'm displaying some results in a UITableView and I'm displaying then UILables of UITableView's cell..
How can I make the UILabel's to adjust the content according to the text. Actually text is not static .It may be changing at run time.So I need to set dynamic size to a UILabel .Ans secondly suppose if the text is null then it should not show any space and immediately the next UILabel should start .How can I make it possible ?
Here is my code..
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,320,800) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentHtmlElement==#"3")
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSMutableDictionary *d = (NSMutableDictionary *) [arr2 objectAtIndex:indexPath.row];
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
UILabel *name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 40)];
name1.font=[UIFont boldSystemFontOfSize:16];
[name1 setTextAlignment:UITextAlignmentLeft];
[name1 setText:[d valueForKey:#"Name"]];
name1.tag = 111;
[cell addSubview:name1];
[name1 release];
UILabel *codetype=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
codetype .font=[UIFont boldSystemFontOfSize:12];
[codetype setTextAlignment:UITextAlignmentLeft];
[codetype setText:[d valueForKey:#"Code"]];
codetype.tag=112;
[cell addSubview:codetype];
[codetype release];
UILabel *id=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
id .font=[UIFont fontWithName:#"arial" size:12];
[id setTextAlignment:UITextAlignmentLeft];
[id setText:[d valueForKey:#"ID"]];
id.tag=113;
[cell id ];
[id release];
UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320,10)];
address .font=[UIFont fontWithName:#"arial" size:12];
[address setTextAlignment:UITextAlignmentLeft];
[address setText:[d valueForKey:#"address"]];
line2.tag=114;
[cell addSubview: address];
[address release];
UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 320, 10)];
city .font=[UIFont fontWithName:#"arial" size:12];
[city setTextAlignment:UITextAlignmentLeft];
[city setText:[d valueForKey:#"City"]];
city.tag=115;
[cell addSubview:city ];
[city release];
}
Here I have set all the UILabels to static size.But what I need is dynamic size.and suppose if the address label is null then it should not show any space and next label i.e city should start immediately next after id ..How is it possible?
I did this task in my code.... you can use this code and do changes according to your code....and let me know if you get any problem.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];
//code to get notification text height
NSString *text = [dicNotification objectForKey:#"message"];
CGSize constraint = CGSizeMake(175, 100.0f);
CGSize size = [text sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//label for message
UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectZero];
lblMsg.tag = 12;
lblMsg.backgroundColor = [UIColor clearColor];
[lblMsg setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lblMsg setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:lblMsg];
}
NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];
//get the message height set frame height to imageView(BubbleMid & Top) & labelMSG
NSString *txt = [dicNotification objectForKey:#"message"];
CGSize constraint = CGSizeMake(175, 2000.0f);
CGSize size = [txt sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
//set frame height to labelMsg
UILabel *lblMsg = (UILabel *)[cell viewWithTag:12];
lblMsg.frame = CGRectMake(15,25, 175, size.height);
[lblMsg setNumberOfLines:size.height/16];
//set labelMsg text
lblMsg.text = [dicNotification objectForKey:#"message"];
return cell;
}
The thing is that you must use heightForRowAtIndexPath. The problem is that it is called prior to cellForRowAtIndexPath, therefore the text is not in the component and you can not simply adjust the component size to the text. So, what you do is calculate the text size using a sizeWithFont.
In case of normal/standard text
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//here you get your text string
NSString* theText = ... ;
//then calculate the size. systemFontOfSize depends on your font size
//yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(yourLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
return textSize.height;
}
In the case of attributed text
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//here you get your text string
NSAttributedString* theText = ...;
//then calculate the size.
//yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(yourLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:NULL];
return rectSize.size.height;
return textSize.height;
}
you can make UILable adjust content with bellow code
CGSize sizeToMakeLabel = [name1.text sizeWithFont: name1.font];
name1.frame = CGRectMake(name1.frame.origin.x, name1.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
and here set second lable like bellow..
UPDATE
if([[d valueForKey:#"address"] isEquelToString:#""]){
UILabel *city=[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
city .font=[UIFont fontWithName:#"arial" size:12];
[city setTextAlignment:UITextAlignmentLeft];
[city setText:[d valueForKey:#"City"]];
city.tag=115;
[cell addSubview:city ];
CGSize sizeToMakeLabel = [city.text sizeWithFont: city.font];
city.frame = CGRectMake(city.frame.origin.x, city.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
}
else{
UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
address .font=[UIFont fontWithName:#"arial" size:12];
[address setTextAlignment:UITextAlignmentLeft];
[address setText:[d valueForKey:#"address"]];
line2.tag=114;
[cell addSubview: address];
CGSize sizeToMakeLabel = [address.text sizeWithFont: address.font];
address.frame = CGRectMake(address.frame.origin.x, address.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
}
and so on for every label..
follow this logic for whole label
i hope this help you...
How may I reproduce the message UITableViewCell style, without using custom cells ?
For the moment, I am using UICellStyleWithSubtitle, and I add accessories, but I can only see the date, and the UITableViewCellAccessoryDisclosureIndicator is hidden.
What I want :
http://www.askdavetaylor.com/4-blog-pics/iphone-text-txt-message-haiti-2.png
What I am doing :
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 45, 15)];
[myLabel setText:[NSString stringWithFormat:#"%#", [table returnDate]]];
[myLabel setAdjustsFontSizeToFitWidth:YES];
[myLabel setTextColor:[UIColor blueColor]];
[myLabel setBackgroundColor:[UIColor clearColor]];
cell.accessoryView = myLabel;
[myLabel release];
cell.textLabel.text = table.title;
cell.detailTextLabel.text = table.subtitle;
For this, you don't have to use UICellStyleWithSubtitle..u could use UITableViewCellStyleValue1 and try with detailTextLabel property of cell like:
cell.detailTextLabel.text = #"Yesterday";
Try this,
cell.detailTextLabel.text = [table returnDate]
instead of using label simply assign the value to detailTextLabel, and give cell style as UITableViewCellStyleValue1.