iPhone: Text width in pixels - iphone

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

Related

How to change UILabel size as per text with limited width

Currently my label is having width of 100 pixels by default.
the text on label varies dynamically
my requirement is that
The label size should change as per text and its width should limited to 300 pixels.
After 300 pixels the text goes to next line and the height of the label has to be increses.
how to achieve this scenario..
self.messageLabel = [[UILabel alloc] init];
self.messageLabel.frame = CGRectMake(0, 0, 100, 20);
self.messageLabel.textAlignment = NSTextAlignmentCenter;
self.messageLabel.font = [UIFont boldSystemFontOfSize:16];
messageLabel.numberOfLines = 0;
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.view addSubview:messageLabel];
to get the text size :
CGSize labelSize = [self.messageLabel.text sizeWithFont:self.messageLabel.font];
And i can able to Change the width of label as per text
self.messageLabel.frame = CGRectMake(0, 0, self.frame.size.width-10, 20);
But how to move the text to next line after every 300 pixels and
label size increase in height as well.
-(CGSize)getLabelSize:(NSString *)text
{
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize;
}
Use this method.
I hope it helps you.

Height when using sizeWithFont:constrainedToSize:lineBreakMode with NSLineBreakByTruncatingTail

When I run the following code (iOS 6.1 SDK):
UIFont *font = [UIFont fontWithName:#"Avenir" size:12.0];
NSString *text1 = #"Short Label";
NSString *text2 = #"Long Label Whose Text Will Be Truncated Because It Is Too Long For The View";
CGSize text1Size = [text1 sizeWithFont:font
constrainedToSize:
CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByTruncatingTail];
CGSize text2Size = [text2 sizeWithFont:font
constrainedToSize:
CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByTruncatingTail];
NSLog(#"Text 1 Size: %f %f", text1Size.width, text1Size.height);
NSLog(#"Text 2 Size: %f %f", text2Size.width, text2Size.height);
The log output is:
Text 1 Size: 62.000000 17.000000
Text 2 Size: 300.000000 34.000000
Why are the heights different, here? In either case, it's one line of text; just one is truncated, the other is not.
Thanks!
The height is different because you gave it CGFLOAT_MAX as the height, so the method tries to figure out what size the text will be by breaking the text into multiple lines. In this case, apparently, two lines will be enough to contain that string in the width supplied.

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 Title Label/Textbox Text Wrapping?

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;

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.