Nav Link On

iPhone Dev Forums > General Development > SDK Coding Help > Wrapping text for a label

Reply

 

LinkBack Thread Tools Display Modes
Old 08-05-2008, 06:22 AM   #1 (permalink)
Junior Member
Default Avatar
 
Join Date: Jul 2008
Posts: 26
Question Wrapping text for a label

Hi,

I am adding a label in my custom tableviewcell. But the text is too long to fit in a single line.So i want it to be wrapped on to the next line.

I used lineBreakMode as UILineBreakModeWordWrap and also set the numberOfLines to 0.

But the text was cut off.

My actual text is
This is a sample text for a label

Before setting these properties the output was like
This is a sample text for...

And after setting the properties it comes out to be
This is a sample text for


Actually i cannot increase the width for the label due to my app design, so i have to display it on multiple lines.

Please let me know if i need to set some other properties or change the existing to some other values..

Thanks,
Akshay.
Akshay Shah is offline   Reply With Quote
Old 08-11-2008, 06:51 AM   #2 (permalink)
Junior Member
Default Avatar
 
Join Date: Jul 2008
Posts: 26
Default

Haven't anybody wrapped the text yet????
Akshay Shah is offline   Reply With Quote
Old 08-12-2008, 12:16 AM   #3 (permalink)
Member
Default Avatar
 
Join Date: Jun 2008
Posts: 56
Default

Can you paste in the code block in which you allocate/initialize the label and add it to the view?
wyvwyv is offline   Reply With Quote
Old 08-12-2008, 04:15 AM   #4 (permalink)
Junior Member
Default Avatar
 
Join Date: Jul 2008
Posts: 26
Default

Hi wyvwyv,

Here my code snippet that does the thing...

mainText = [self newLabelForMainText:YES setSizeFor:1];
mainText.textAlignment = UITextAlignmentLeft;
[self addSubview:mainText];
[mainText release];


- (UILabel *)newLabelForMainTextBOOL)main setSizeForNSInteger)sizeFor{
...
newLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,2,200,30)];
newLabel.lineBreakMode = UILineBreakModeWordWrap;
newLabel.numberOfLines = 0;
newLabel.opaque = YES;
newLabel.textColor = primaryColor;
newLabel.highlightedTextColor = selectedColor;
newLabel.font = font;
..
return newLabel;
}

Please help me out for this..
Actually the text fits in if i reduce the font size.
But i dont want to do that...
Akshay Shah is offline   Reply With Quote
Old 08-12-2008, 05:20 AM   #5 (permalink)
Member
Default Avatar
 
Join Date: Jun 2008
Posts: 56
Default

Here is code I use that wraps the text in the label just fine.

Code:

You must Login or Register to view and contribute code! This is done to increase participation in helping one another out, if you have been helped please pass on the favor.

labelInstructions = [[UILabel alloc] initWithFrame:CGRectMake(20,20,240,200)];
labelInstructions.font = [UIFont systemFontOfSize: 16];
labelInstructions.lineBreakMode = UILineBreakModeWordWrap;
labelInstructions.numberOfLines = 0;
labelInstructions.backgroundColor = [UIColor clearColor];
labelInstructions.text = @"..long text string..";
[self.view addSubview:labelInstructions];
If you ask me, the problem is that your label's veritcal height is too small. You set it to 30, which is probably the height of your font (you didn't include your font instantiation, so I don't know for sure). You probably can't fit more than one line of text in that label vertically. Try changing the label height to 60 or 100.
wyvwyv is offline   Reply With Quote
Old 08-12-2008, 12:58 PM   #6 (permalink)
Junior Member
Default Avatar
 
Join Date: Jul 2008
Posts: 26
Default

hi wyvwyv,

It was the same what you said... that the label's vertical height was small.. however my font size was 15 but still it was giving problem.
Now i changed the label height to 40 and the wrapped text is shown...

However is it possible that my cell height would be changed according to the height of the text in that cell..
I mean if text fits in a single line then the row size would be for single line, however if the text is wrapped to 2 or 3 lines then the row height is changed accordingly.

The reason i need this is that if i have 3 rows in my table out of which only 1 row has wrapped text of 3 lines then the other two rows have lots of free space which looks very bad...

Thanks...
Akshay Shah is offline   Reply With Quote
Old 08-12-2008, 04:26 PM   #7 (permalink)
Member
Default Avatar
 
Join Date: Jun 2008
Posts: 56
Default

So these labels are going inside of UITableCellViews? And you want to make the height of the table cell appropriate to the height of the text?

Not simple, but I think it is possible. You'll need to subclass UITableCellView (which I'm guessing you've done).

The table itself should have a delegate with the tableView:heightForRowAtIndexPath method. This method is what you use to set the height for each row.

What you also need is an array of your subclassed UITableCellViews somewhere (which is what you would use to populate the table in the tableView:cellForRowAtIndexPath: method).

The subclassed table cell must have a public member or property that says how much height that cell will want.

You can use the textRectForBounds:limitedToNumberOfLines: of the UILabel to get the drawing rectangle of the label. Use the height component of that size to determine the height required, and pass it back to the table delegate that determines the height of the cell.
wyvwyv is offline   Reply With Quote
Reply

iPhone Dev Forums > General Development > SDK Coding Help


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0 RC1