iPhone Title Label/Textbox Text Wrapping? - iphone

I have a PSTitleValueSpecifier field in my application's settings that displays a long character string.
After about 20 digits it gets cut off and I'd like to wrap the text or increase the height of the field so that any amount of characters can be displayed. Can this be accomplished? If so, how?

I'm not sure if I completely understand your question, but here's what I do to achieve text wrapping in a label. You may be able to adapt some of this.
NSString *yourLongStringOfText = #"yourLongStringOfText";
UIFont *yourFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [yourLongStringOfText sizeWithFont:yourFont
constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 5;

Related

Unable to control the width of UITextView

I am making an application where I need to create a UITextView on the screen. The height and width of the TextView should go on increasing as the user types i.e. the height and width of the TextView should change according to its content.
However I have been able to control the height of the TextView through the myTextView.ContentSize.height. But I could not find a way to control the width of the TextView according to its content.
Even myTextView.contentSize.width doesn't work for me.
I am doing all my frame calculations within textViewDidchange delegate method of UITextView.
Any suggestions??
In the textViewDidChange,
CGSize constraintSize;
CGSize labelSize;
constraintSize = CGSizeMake(myTextView.frame.size.width, 480);
NSString *text = myTextView.text;
text = [text stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
text = [text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
if([text stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding])
{
text = [text stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}
// Note that in the below line you must use the font as exact as you use for textview
labelSize = [text sizeWithFont:[UIFont systemFontOfSize: 16] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
In the labelSize you can find the expected size of the text view.
You can set the frame of myTextView as
myTextView.frame = CGRectMake(myTextView.frame.origin.x,myTextView.frame.origin.y,labelSize.width,labelSize.height);
Think this will works for you. Try it. Hope this helps.

UILabel size to fit

I have a problem involving UILabel's sizeToFit method:
UILabel *questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,320,320)];
questionLabel.lineBreakMode = UILineBreakModeWordWrap;
questionLabel.backgroundColor=[UIColor clearColor];
questionLabel.textAlignment=UITextAlignmentLeft;
questionLabel.textColor=[UIColor blackColor];
questionLabel.tag=1;
questionLabel.font=[UIFont systemFontOfSize:13];
questionLabel.numberOfLines = 0;
[questionLabel sizeToFit];
[myView addSubview:questionLabel];
I had written this code for displaying my data. But if I write: [questionLabel sizeToFit] my data does not display properly. If I remove [questionLabel sizeToFit] then it is displaying but it only shows half the data.
Thanks and Regards.
NSString *yourString = #"write your label text here";
CGSize s = [yourString sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
questionLabel.frame = CGRectMake(0, 0, s.width, s.height);
Check if it helps.
I think it's best to use taus-iDeveloper answer to compute the size of a label.
I just want to say that the reason your code is not working is because you didn't set text to your UILabel so sizeToFit returns CGSizeZero (so it doesn't appear on screen). You have to set text before using sizeToFit.
I found that if AutoLayout is on then size to fit not work
i googled d above problem and came across some info that sizeToFit seems to be a bug and it has been reported to apple already.
So as a workaround u can use this code:
NSString * myText = [NSString stringWithString:#"some text"];
CGFloat constrainedSize = 265.0f;
UIFont * myFont = [UIFont fontWithName:#"Arial" size:19];
CGSize textSize = [myText sizeWithFont: myFont
constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
CGRect labelFrame = CGRectMake (0, 0, textSize.width, textSize.height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
[label setFont:myFont];
[label setText:myText];
I've found that when using sizeToFit with a UILabel, in InterfaceBuilder you need to change the Autoshrink property from 'Fixed Font Size' to 'Minimum Font Size'. I usually then set its value to 0.5 to be sure its working properly.
just make sure you increase the number of lines of the label. Instead of
questionLable.numberOfLines = 0; make use of
questionLable.numberOfLines = 4;
As it will force the system to compute the smallest width possible for 4 lines.
You can achieve this via the Xib file too..

iPhone: Text width in pixels

I have UILabel (286, 50). When I set some text with font Helvetica 16, label divides text on 2 rows. Thats grate. Now I want to get this text width in pixels.
CGSize uiLabelSize = CGSizeMake(286, 50);
return [text sizeWithFont:[UIFont fontWithName:#"Helvetica" size:16]
constrainedToSize:uiLabelSize lineBreakMode:UILineBreakModeWordWrap].width;
But looks like this method dont want to divide this text on 2 rows. it returns for me 284 px.
Any idea how to be ?
return [text sizeWithFont:[UIFont
fontWithName:#"Helvetica" size:16]].width;
This method not work for me. Because if in first row last word is too long, than UILineBreakModeWordWrap will move last word on second row...
Sorry for hard explanation, let me know if I need to be more clear...
Replace 50 with an unconstrained height:
CGSize textSize = [text sizeWithFont:font
constrainedToSize:CGSizeMake(286.0, FLT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
edit:
NSString *text = #"what's wrong with your hair? it's like a science experiment back there. Don't rub more salt in the wound.";
UIFont *font = [UIFont fontWithName:#"Helvetica" size:16];
CGSize textSize = [text sizeWithFont:font
constrainedToSize:CGSizeMake(286.0, FLT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"%f,%f", textSize.width,textSize.height);
This outputs 272 width (because it is word wrapping before 286 pixels) and 60 height (because it is taking two lines).
UILabel * testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 286.0f, 50.0f)];
[testLabel setText:#"ab"];
NSLog(#"%f",[testLabel.text sizeWithFont:[UIFont fontWithName:#"Helvetica" size:16] constrainedToSize:CGSizeMake(286.0f, 50.0f) lineBreakMode:UILineBreakModeWordWrap].width);
It looks interesting. So I tried in my xcode and the code above works for me. When set text to #"a", the width returns 9, and #"ab" returns 18.
Have you set the text? Or you may mix the UILabel with its text?
I'm not sure this is that you want, just give some tips.
Try something llike this:
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:label.lineBreakMode];
labelSize gets the size of label constrained by text
I'm not sure if it is what you need..

heightForRowAtIndexPath for longer NSStrings

I have a UITableView (Grouped!) and need to calculate the height of two styles of cells: UITableViewCellStyleDefault and UITableViewCellStyleValue2.
This is how I do it for UITableViewCellStyleDefault:
CGSize textSize = {300.f, 200000.0f};
CGSize size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);
And for UITableViewCellStyleValue2:
CGSize textSize = {207.f, 200000.0f};
CGSize size = [myTextString2 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);
My issue it that they return incorrect heights and I think it's the textSize where I use incorrect numbers. With long texts, the bottom part gets cut short (usually just a line with a few words), and for both CellStyles they have weird spacing above and below the text in the cell.
For UITableViewCellStyleValue2 I got the width size (207.0f) from making the text.backgroundColor red and then just calculating the size of the box. 300.0f width for UITableViewCellStyleDefault is how big the cell is with UITableViewStyleGrouped.
Does anyone know which values I need to use to properly calculate the size of an NSString and thereby get appropriate height for my cells?
Thanks
Here is the code I am using for this. It works like a charm for one type of cell. It may have some useful parts for your application.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {
AppAppDelegate *appDelegate = (AppAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *Text = ([[appDelegate.myTextSectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:#"Text"]);
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 15;}
When you create your cell, are you using the same font as the one you use to measure?
I used the tutorial on this page and everything worked for me. It might be useful to you too:
It seeems you've figured out that you were using the wrong text size. You might want to just use the font and lineBreakMode properties of the label to avoid this problem in the future, especially if you change them in the cell at a later time. Also, for readability's sake, I would avoid adding to the height before returning a number. Instead I'd try something like this:
CGSize textSize = CGSizeMake( 300.0, 1979 );
CGSize size = [ myTextString1 sizeWithFont:[[ cell textLabel ] font ]
constrainedToSize:textSize
lineBreakMode:[[ cell textLabel ] lineBreakMode ]];
result = MAX( size.height + 30, 44.0f );
I used 1979 because according to the documentation, one should not return values larger than 2009.
If you want to calculate the height properly for cellvalue2 you should use:
[UIFont boldSystemFontOfSize:15.0f] and as linebreakmode: NSLineBreakByTruncatingTail
If you don't use bold, the text will fill correctly but you will lose the correct vertical padding.
The rest of your calculation are correct.
When is working for me is to compute the constraintSize based on the width of the table view:
CGSize constraintSize = CGSizeMake(tableView.frame.size.width * 0.6, 2009);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
Easiest way to do this:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat result = 10.0f;
if(indexPath.section == 1) // here i compare the sections
{
result = 400.0f;
}
return result;
}
Return the values based on the sections you have. This will give you custom row heights.

How do I fix a multi-line UILabel that is overflowing its containing UITableViewCell?

I'm having trouble displaying a multi-line UILabel in a custom UITableView cell.
I'm currently using this to calculate both the height of the cell...
NSString *cellText = [howtoSection objectAtIndex:row];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
...and this is for the label itself.
// Calc the height
NSString *cellText = [howtoSection objectAtIndex:row];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cell.textLabel.font constrainedToSize:constraintSize lineBreakMode:cell.textLabel.lineBreakMode];
// Create the label frame
CGRect newFrame = cell.contentLabel.frame;
newFrame.size.height = labelSize.height;
cell.contentLabel.frame = newFrame;
[cell.contentLabel setText:[howtoSection objectAtIndex:row]];
Everything is working as planned except that the label is being pushed down and out of its cell. If it wasn't for this apparent top margin everything would fit.
Here's a link to a picture of what I'm seeing in the simulator...
iPhone Rendering Bug
Any help here would be greatly appreciated.
I don't see you setting cell.contentLabel.font in the code you show.
Also the label size calculation uses cell.textLabel.font to calculate its size but renders using the contentLabel variable.
Is it possible you're rendering with a different font than the calculation?
How are you adding your label to the contentView? It looks like the original positioning is wrong, since the height looks to be calculated correctly. If you comment out assigning the new frame, is the label in the correct position? My wager is that it isn't.