Cell TextLabel multiline not working anymore - iphone

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?

Related

numberOfLines of UILabel doesn't work within cell

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:.

UILabels complete text not visible

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....

Resize UITableView font when textLabel length is over 20 chars?

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];

resize title in uitableviewcellstyledefault

i've been trying around for a while now and cant seem to find a solution. i am using a UITableViewCellStyleDefault cellstyle in my tableview, and am trying to get the font to resize when the text gets too long.
cell creation
static NSString *CellIdentifier = #"thisMonthCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
[cell.textLabel setAdjustsFontSizeToFitWidth:YES];
[cell.textLabel setMinimumFontSize:14];
UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 10, 100, 20)];
[valueLabel setBackgroundColor:[UIColor clearColor]];
valueLabel.tag = 1001;
[valueLabel setTextAlignment:UITextAlignmentRight];
[cell addSubview:valueLabel];
}
Expense *expense = [[self.dataHandler monthExpenses]objectAtIndex:indexPath.row];
UILabel *value = (UILabel*)[cell viewWithTag:1001];
[cell.textLabel setText:[expense name]];
if ([[expense value]doubleValue] > 0) {
[value setText:[NSString stringWithFormat:#"+%.2f",[[expense value]doubleValue]]];
[value setTextColor:[self greenColor]];
}
else{
[value setText:[NSString stringWithFormat:#"%.2f",[[expense value]doubleValue]]];
[value setTextColor:[self redColor]];
}
return cell;
but somehow the textLabel wont resize if the text is too long.
here is a screenshot demonstrating the problem:
any ideas?
UPDATE i managed to achieve my goal by removing the standardLabel and adding a custom one,.. weird that it would not work with the standard one.
Try this cell.textLabel.numberOfLines = 0; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap
Use following two line of code also when you are creating value label.
valueLabel.lineBreakMode = UILineBreakModeWordWrap;
valueLabel.numberOfLines = 0;
EDITED-
To change the height of cell -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellText = #"oooooooooooooooooooo"; //Text that you are using
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:16.0]; //Whatever font you are using.
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 25.0; //25.0 is offset, you can change as per need
}

Cant set the height of my uitableviewcell right

I set the size of my UITableCellĀ“s with this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * vergleich = [nachricht objectAtIndex:indexPath.row];
CGSize size = [vergleich sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14]
constrainedToSize:CGSizeMake(268, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
return size.height + 30;
}
nachricht is a NSArray which contains all the messages. The code just looks how long the message (with a specified font) is and calculate the height. I set + 30, because over the message(UITextView) is a UIlabel.
The UITextView, which should contain the messages, get the size with this code:
- (void)setTweetText:(NSString *)_tweet;{
CGSize size = [_tweet sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14]
constrainedToSize:CGSizeMake(268, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
[textText setFrame:CGRectMake(55, 25, 268, size.height + 10)];
textText.text = _tweet;
[textText sizeToFit];
textText.dataDetectorTypes = UIDataDetectorTypeLink;
}
Now there is a problem and I don't know why: The UITextView is bigger then the cell, even if I set the size of the cell height there is a unpleasant distance between the TextView and the next cell. Why doesn't he get the right height for some cells. Here is an example:
alt text http://img34.imageshack.us/img34/214/bildschirmfoto20100120uw.png
All I can say is in my cellForRowAtIndexPath I use the following and it works. My heightForRowAtIndexPath is nearly identical to yours.
double d = [self tableView:table heightForRowAtIndexPath:indexPath];
UILabel* label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 280, d-20)] autorelease];
label.numberOfLines = 100;
label.lineBreakMode = UILineBreakModeWordWrap;
[label setFont:[UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]];
label.text = [descriptions objectAtIndex:indexPath.section];
[cell.contentView addSubview:label];
one possibility is you have set the CGSize width to be different in each function, for row height you have CGSizeMake(268, MAX_HEIGHT) and for setTweetText it is CGSizeMake(262, MAX_HEIGHT)
its not a good idea to sizeWithFont from that method, its fairly heavy and will chop up your scrolling performance. if possible calculate the heights before and store them for quick access from that method