Partially displaying the Title in the tableView Cell? - iphone

I'm tried to display the Title Data in the Tableview Cell it displayed but partially (...) i want to display full title in the table cell may be in two or three line i used customize cell and use the label and set label property but still data displayed partially
titleLabel.text = aNewsInfo.title;
Can any one suggest me the way to overcome this prob...
Thanks in Advance...

Try this code:
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2; // 2 lines ; 0 - dynamical number of lines
myLabel.text = #"Lorem ipsum dolor sit\namet...";

Maybe you can try:
[titleLabel sizeToFit];
And the you need to adjust the height of the cell with:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

The label's property needs to be set
adjustsFontSizeToFitWidth
A Boolean value indicating whether the font size should be reduced in order to fit
the title string into the label’s bounding rectangle.
#property(nonatomic) BOOL adjustsFontSizeToFitWidth
And
numberOfLines
The maximum number of lines to use for rendering text.
#property(nonatomic) NSInteger numberOfLines
Discussion
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.
When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

Hi try to add label in your cellforRow method
CGSize labelsize;
UILabel *commentsTextLabel = [[UILabel alloc] init];;
commentsTextLabel.tag =50;
[commentsTextLabel setNumberOfLines:0];
[commentsTextLabel setBackgroundColor:[UIColor clearColor]];
NSString *text=[#"dasdasdasdasfasfasfasfasfasfsafasfsafasfasfasfasfsafsafasfasfsaf"];
[commentsTextLabel setFont:[UIFont fontWithName:#"Helvetica"size:14]];
labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
commentsTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
[cell.contentView addSubview:commentsTextLabel];
[commentsTextLabel release];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelsize;
UILabel * textDesc1 = [[UILabel alloc] init];
[textDesc1 setNumberOfLines:0];
textDesc1.text=[#"dasdasdasdasfasfasfasfasfasfsafasfsafasfasfasfasfsafsafasfasfsaf"];
[textDesc1 setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
labelsize.height=labelsize.height+35;
[textDesc1 release];
return (CGFloat)labelsize.height;
}

Related

how to make UILabel dynamically without overlapping with other UILabel

I've tried to search online, but haven't found a clear answer for my issue so I've come to ask for your expert advice. I have a view with 2 labels on it. Both label will display different string length from the plist.
When i run the app, the label will overlapped with other label depending on the string length.
Below is the screenshot for my problem
You have to change your secondLabel origin.
CGRect frame = secondLabel.frame;
frame.origin.y= firstLabel.frame.origin.y + firstLabel.frame.size.height;
[secondLabel setFrame:frame];
Better option is to use UITextView instead of UILabel but if you still want to go with lable then
with the use of below code you can find the height of the text and can set your lable's frame according to that height
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
here contentWidth is the width of your label and CELL_CONTENT_MARGIN = 10;
You need to set the 'Y' of second label. Take the Height of first label text and then set it to the Second Label 'Y'.
Hope it'll help you.
CGSize LblSize=[[Label1 text] sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(320.0f, 400.0f)];
UILabel *Label2=[[UILabel alloc] initWithFrame:Label2Rect];
CGRect Label2Rect=[Label2 Frame];
Label2Rect.origin.y=LblSize.height+30.0f; //add some extra spaces, I have added 30.0f here
[Label2 setFrame:Label2Rect];
Ey, you can solve it in many ways.
For example, you can fill your first label with the dessired text and then call to
[label1 sizeToFit]
With that call, your label now has the proper size, adapted to the lenght of your text. Now you can just place your second label after your first one.
label2.frame = CGRectMake (x, label1.frame.size.height + ..., .....)
Hope it helps!

UILabel Multiple Line in iOS6

I have to show multiple line in UILabel (If text is large). Below is my code. I am using separate properties for different iOS versions. Please help me out..
labelLocation.numberOfLines=2;
labelLocation.font=[UIFont systemFontOfSize:25];
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=6) {
labelLocation.lineBreakMode=NSLineBreakByTruncatingTail;
labelLocation.minimumScaleFactor=10.0/[UIFont labelFontSize];
}else{
labelLocation.lineBreakMode=UILineBreakModeTailTruncation;
labelLocation.minimumFontSize=10;
}
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
these two line together works
labelLocation.numberOfLines=0;
labelLocation.lineBreakMode = NSLineBreakByWordWrapping;
you can set
yourlabelname.lineBreakMode = NSLineBreakByWordWrapping;
yourlabelname.numberOfLines = give how many lines you want for your label(e.g.2,3,etc...)
and check if your outlet is set properly.
Try this labelLocation.numberOfLines=0;
I suppose, that your label has to small height. Two lines in systemFontOfSize 25 need height about 60.
If label is to small, system doesn't wrap line.
change ur code to this
labelLocation.numberOfLines=0;
labelLocation.font=[UIFont systemFontOfSize:40];
labelLocation.lineBreakMode=NSLineBreakModeWordWrap;
labelLocation.text=#"Can we make UILabeltext in 2 lines if name is large";
I would personally recommend you to calculate the height required to show the text and then show it onto the label...never hard code text display components such as UITextView and UILable.
NSString *str = #"This is to test the lable for the auto increment of height. This is only a test. The real data is something different.";
`UIFont * myFont = [UIFont fontWithName:#"Arial" size:12];//specify your font details here
//then calculate the required height for the above text.
CGSize lableSiZE = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(240, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
//initialize your label based on the height you got from the above..you can put whatever width you prefer...
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lableSiZE.width, lableSiZE.height)];
myLable.text = str;
myLable.numberOfLines=0;
myLable.font=[UIFont fontWithName:#"Arial" size:12];
//myLable.backgroundColor=[UIColor redColor];
myLable.lineBreakMode = NSLineBreakByWordWrapping;

UITableView - getting the width of cells to fit text?

I'm coding a custom UITableViewCell object and I've implemented layoutSubviews to resize the cell when its contents is updated.
To implement heightForRowAtIndexPath, I'm calculating the height of the cell within the custom UITableViewCell object. I'm using NSString sizeWithFont to calculate the size of the cell based on the text within a UILabel and the width of a cell in the UITableView.
But how do I get the width of a cell in the UITableView?
Right now, I'm passing in the table view to the object and using the frame. But the width is of the whole table not the individual cells. Is there something I'm missing here?
Thanks in advance.
EDIT3
I was thinking, if it's really not possible, just test portrait or landscape orientation then set the width manually (ipad just has 2 different orientations)..
EDIT2
There have been some questions about 'why are you doing it this way xxxx'. I understand maybe there's a better way to achieve what I'm doing, creating a custom cell that can calculate its own height with varying text length. If there's a better way of doing it I'm all ears :)
EDIT
http://img845.imageshack.us/img845/7792/screenshot20121129at193.png
+ (CGFloat)getHeightForCellWithText:(NSString *)text isExpanded:(BOOL)expanded tableView:(UITableViewController *)tv {
ProposalViewCell *cell = [[ProposalViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:#"NormalCell" tableView:tv];
[cell setLabelText:text];
[cell setExpanded:expanded];
[cell layoutSubviews];
return cell.primaryLabel.frame.size.height + cell.readmoreButton.frame.size.height + cell.sendmessageButton.frame.size.height +30;
}
- (void)layoutSubviews {
[super layoutSubviews];
_primaryLabel.numberOfLines = 0; // multiple lines
// size of expanded text label
// sizeWithFont: if text doesn't fit, it is truncated
CGSize expandedSize = [_primaryLabel.text sizeWithFont:myFont constrainedToSize:CGSizeMake(tableView.view.frame.size.width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
// size as expanded by default
_primaryLabel.frame = CGRectMake(10, 10, expandedSize.width, expandedSize.height);
if (expanded==NO) {
// size of summary text label
_primaryLabel.numberOfLines = 10;
CGSize summarySize = [_primaryLabel sizeThatFits:_primaryLabel.frame.size];
_primaryLabel.frame = CGRectMake(10, 10, summarySize.width, summarySize.height);
}
_readmoreButton.frame = CGRectMake(10, _primaryLabel.frame.size.height+10, 225, 25);
_sendmessageButton.frame = CGRectMake(10, _primaryLabel.frame.size.height+10+_readmoreButton.frame.size.height+10, 225, 25);
}
To get the width of the cell just do this...
cell.contentview.frame.size.width

iPhone:How to change rowheight by UILabel's content height

I want to dynamicaly set the cell's rowheight to the UILabel's height (which is cell's subview).
The difficulty is UILabel needs to get its height by checking its string content
Two problems:
1- How can I set correctly the numberofLines in UILabel depending on the string lenght?
2- How can I reflect this UIlabel height to row.height because delegate method heightForRowAtIndexPath is called before the cellForRow method?
here is some code, whcih I am sure I do somethinsg wrong..
Nsstring st= "very long text...";
CGSize theSize = [st sizeWithFont:font constrainedToSize:CGSizeMake(250.0f, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGRect cg = CGRectMake(0.0f, 0.0f, 250.0f, theSize.height);
UILabel *label=[[UILabel alloc] initWithFrame:cg];
label.textAlignment=UITextAlignmentLeft;
label.text=st;
[label setNumberOfLines:10];// this is trouble!! how to set this!?
[cell addSubview:label];
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {
CGSize labelSize = CGSizeMake(200.0, 20.0);
if ([string length] > 0)
labelSize = [string sizeWithFont: [UIFont boldSystemFontOfSize: 17.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
return 24.0 + labelSize.height;
}
You need to implement heightForRowAtIndexPath. In my sample code, I returned 24 + the height of the label
(Mashup of my comments)
1- How can I set correctly the numberofLines in UILabel depending on the string lenght?
UILabel's numberOfLines is the maximum number of lines. "To remove any maximum limit, and use as many lines as needed, set the value of this property to 0."
2- How can I reflect this UIlabel height to row.height because delegate method heightForRowAtIndexPath is called before the cellForRow method?
You need to implement heightForRowAtIndexPath: and return the appropriate height for the row. You can move the computation of theSize to a helper method and use it from cellForRowAtIndexPath: too.
You can have a separate textForRow: function that you can both use to set the text of the label, and also to calculate the size of the label (and the cell) in the delegate method (much like you have already done)

Calculating multiline text height for UILabel/UITableViewCell: different results when calculating vs actual drawing

This general topic has been asked here multiple times: how to render UITableViewCells with varying amount of text and thus varying height. The canonical answer is: you calculate the height in table view controller delegate in heightForRowAtIndexPath using sizeWithFont:constrainedToSize:lineBreakMode:. Later, the cell gets drawn, and you use something like [label sizeToFit] if needed, and all works like magic.
My problem: I am getting wrapping for some cells because sizeWithFont: returns different dimensions from actual drawing.
A specific example:
The text is this: "People forget that #BillGates had a sexy 1/4-inch thick slate back in 1993 from NEC. Whatever happens this week will NOT be about hardware!"
CGSize theSize = [text sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(310.0f, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"calculated size for %#: %f, %f",text, theSize.width, theSize.height);
This returns: 306.000000, 84.000000. (I.e 4 rows with 17px font and 4px linespacing, 21px leading.) Good.
However, later when actually drawing the cell:
label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:#"%#", text];
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont systemFontOfSize:17.0f];
CGSize labelSize;
labelSize = label.frame.size;
NSLog(#"label size before resizing: %f, %f", labelSize.width, labelSize.height);
[label sizeToFit];
labelSize = label.frame.size;
NSLog(#"label size after resizing: %f, %f for text %#", labelSize.width, labelSize.height,text);
(UILabel is loaded as part of UITableViewCell from NIB. In IB I set it to 310px wide.)
This should return exactly the same size as above. Instead, I get 281.000000, 105.000000 as the dimensions after sizeToFit call. It is now 5 lines at drawing time instead of 4, and the text spills over, I see the spillover in the UI.
So, for the same text, I am getting two different dimensions calculated, and can't figure it out. Is it something about UILabel? Does it have some inner margins? This keeps happening for some texts but not others, and I have not traced it to something particular about the strings; seems random. This topic highlights that there are two processing passes: calculating height vs actual drawing. This is consistent with what I'm seeing. But I don't understand what exactly is going on or how to fix it.
The question: why am I seeing two different calculated sizes, and how do I fix it?
Of course, the solution is obvious 30 seconds after posting. Maybe useful to others too...
The size by sizeWithFont: was correct. The sizes I calculated in the above way were incorrect, because [label sizeToFit] reduces the width of the label's frame. At subsequent calls to the same code, it started off with the frame that may already have been reduced.
The fix was to simply reset the frame width to a known good width before sizing to fit:
CGRect labelFrame = label.frame;
labelFrame.size.width = 310;
label.frame = labelFrame;
[label sizeToFit];
For multiline labels you need set
cell.textLabel.numberOfLines = 0;
and then
[cell.textLabel sizeToFit];
But for pretty view you need add some padding pixels. And your app will be awesome!
titleSize = [title sizeWithFont:[UIFont systemFontOfSize:(CGFloat)17.0]
constrainedToSize:CGSizeMake(280, 2000)
lineBreakMode:NSLineBreakByWordWrapping];