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..
Related
I need help with this peculiar problem. I have a multiple choice question app and I have the choices as UITextview. Sometimes, choice D gets cut in half for whatever reason.
Screenshot:
Not sure what's going on here. I basically have the UITextView frame adjust to its contentSize.
CGRect dFrame = choiceD.frame;
dFrame.size.height = choiceD.contentSize.height;
choiceD.frame = dFrame;
Any ideas? Thanks in advance.
Caculate the size of string:
NSString *choiceDString = #"Equal the present value....";
CGSize size = [choiceDString sizeWithFont:[UIFont systemFontOfSize:CHOICE_FONT_SIZE] constrainedToSize:CGSizeMake(CHOICE_WIDTH, 100000)];
Init a label to content the choice string:
UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,size.width,size.height)];
choiceDLabel.text= choiceDString;
Add the subview label for button:
[button addSubview:choiceLabel];
Use this code..Yo have define height of label according to your text length...
NSString *summary;
summary = #" your text";
CGSize sizeofbuttonorlable = [summary sizeWithFont:[UIFont systemFontOfSize:30]
constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, sizeofbuttonorlable.height);
UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:frame];
choiceDLabel.text= summary;
[button addSubview:choiceLabel];
Hope, this will help you...chill
My suggestion is to first Calculate the size of the text entered by you in the textView like:-
//Give the maximum size of label which that label can have.
CGSize maximumLabelSize = CGSizeMake(300,500);
CGSize expectedLabelSize = [Label.text sizeWithFont:Label.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
//adjust the label the new height.
CGRect newDescFrame = Label.frame;
newLabelFrame.size.height = expectedLabelSize.height;
NSLog(#"%f",newLabelFrame.size.height);
//adjust the label the new width.
newLabelFrame.size.width = expectedLabelSize.width;
NSLog(#"%f",newLabelFrame.size.width);
//Set the label size according to the new height and width.
label.frame = newLabelFrame;
Write above mention code after entering the text in the textView.
Hope it helps.Thanks :)
I have a view that is pushed onto the Screen via navigation controllers:
Inside is a UIScrollView.
Then inside the UIScrollView are a few static objects like Images and Labels.
Then comes the hard bit, There is a UITextView with its text loaded from different text files of varying length.
I need to be able to have the UITextView size dynamically to its contents, and the same for the UIScrollView. Is this possible?
float length = [yourText length];
textview.frame = CGRectMake(44, 87, 923, ceilf(length/142)*25);
Here 25 is the constant value assumed as text font width. From this you can set scrollview frame reference to the textview frame.
You can do that with the help of following code. I had done that code for Label and same way you can do that with the help of text-field.
NSString *cellText = "Text Of Your Text-Field";
UIFont *cellFont = [UIFont fontWithName:#"Your Font Name" size:FONT_SIZE];//UIFont *cellFont = [UIFont fontWithName:#"Helvetica-Bold" size:13.0];
CGSize constraintSize = CGSizeMake(#"Width Of Your Text-Field", MAXFLOAT);//CGSize constraintSize = CGSizeMake(220.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
int height = labelSize.height;
frame.origin.x = Starting Position of X;
frame.origin.y = Starting Position of Y;
frame.size.width = Width Of Your TextField;
frame.size.height = height;
UILabel *lblName = [[[UILabel alloc] initWithFrame:frame] autorelease];
lblName.numberOfLines = 0;
lblName.lineBreakMode = UILineBreakModeWordWrap;
lblName.textAlignment = UITextAlignmentLeft;
lblName.textColor = [UIColor whiteColor];
lblName.backgroundColor = [UIColor clearColor];
lblName.font = [UIFont boldSystemFontOfSize:13.0];
And same way you can do For the Scrollview. Just you have to set the Frame of that scrollview and you are done.
Yes you could do something like this:
if ([textView length] > int//any number you want) {
textView.frame = CGRectMake(//just adjust the size an position here);
scrollView.contentSize = CGSizeMake(//adjust scrollView size)
}
else if ([textView length] > int//just another number) {
// you can continue looping that for how often you want
}
For the int in the if statement you check how long the text is. Based on that you adjust the size of both the scrollView and textView.
Problem: Determine the size (number of lines) a UILabel needs, assuming the width is 300 px. The string is longer, so I set the lineBreakMode to UILineBreakModeWordWrap and invoked sizeThatFits to try to determine the size. But it gives a width of 457 px in a single line, rather than the expected 300px in two lines.
Please see:
CGSize available = CGSizeMake(300, INFINITY);
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 400)] autorelease];
label.text = title;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:kBoldFont size:kTitleFontSize];
label.numberOfLines = 3;
CGSize sizedtoFit = [label sizeThatFits:available];
But I find that the sizedtoFit variable has a width of 457 pixels and a height of 22 px, and the UI displays a single line with clipped text. I expect a width of 300 pixels, and a height of 44 px for two lines.
The UILabel doc for numberoflines says:
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.
I tried various combinations of:
Passing CGRectZero to the init function, passing 300x400 or 300 x infinity.
Setting the frame after creation rather than passing it to the init function.
Invoking [sizeToFit] and hoping it calculates the height assuming present width, but it doesn't.
Calling sizeToFit and then calling sizeThatFits`.
Invoking layoutIfNeeded.
None of them works. What am I doing wrong, or is this is bad bug where the documentation and the framework implementation don't agree? Thanks.
I had the same problem, size that fits simply ignores the size... /:
I ended up using:
CGRect textSize = [UILabel textRectForBounds:CGRectMake(0, 0, 300, CGFLOAT_MAX)
limitedToNumberOfLines:3];
Works like a charm... :)
The documentation says you shouldn't call it directly, but i've been using it for a while, with approved submitted apps, and everything is just awesome... :)
Have you tried the sizeWithFont: constrainedToSize: lineBreakMode: method?
For example:
CGSize sizeToFit = [title sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:label.lineBreakMode];
I found Ian L's answer best using -sizeWithFont:constrainedToSize:lineBreakMode:, unfortunately sizeWithFont: is deprecated under iOS7.
This is how sizeWithFont: works for a UILabel subclass in iOS7:
NSRange range = NSMakeRange(0, self.attributedText.length);
sizeToFit = [self.text boundingRectWithSize:self.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:[self.attributedText
attributesAtIndex:0 effectiveRange:&range] context:nil].size;
This is all deprecated. Use boundingRectWithSize
I think you are getting unexpected results because you are not taking into consideration the UILabel's font. Try the following:
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 400)] autorelease];
label.text = title;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:kBoldFont size:kTitleFontSize];
label.numberOfLines = 0;
CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width,FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label.frame = CGRectMake(label.frame.origin.x,label.frame.origin.y,label.frame.size.width,size.height);
There is no solution for ios5 for sizeToFit. You may use other solutions like sizeWithFont etc. In ios6, the issue is fixed. However, I have this workaround for my solutions:
int lineCount = myLabel.numberOfLines;
myLabel.numberOfLines = 0;
[myLabel sizeToFit];
myLabel.numberOfLines = lineCount;
And it works. Beware that for my situation, width of my label is fixed and I only need sizeToFit for adjusting height.
In my project, there is a UILabel with text. The font size is 16pt. The text contents are changed depending on different cases. I hope it can automatically adjust the width of UILabel to fit the total width of texts without stretching.
Is it possible?
This assumes you have already set the font:
label.text = #"some text";
[label sizeToFit];
You will also need to define a maximum width, and tell your program what to do if sizeToFit gives you a width greater than that maximum.
Here's how to do it, suppose the following messageLabel is the label you want to have the desired effect. Now, try these simple line of codes:
// Set width constraint for label; it's actually the width of your UILabel
CGFloat constrainedWidth = 240.0f;
// Calculate space for the specified string
CGSize sizeOfText = [yourText sizeWithFont:yourFont constrainedToSize:CGSizeMake(constrainedWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,constrainedWidth,sizeOfText.height)];
messageLabel.text = yourText;
messageLabel.numberOfLines = 0;// This will make the label multiline
NSString *txt1=#"I am here.";
CGSize stringsize1 = [txt1 sizeWithFont:[UIFont systemFontOfSize:14]];
[label setFrame:CGRectMake(x,y,stringsize1.width,hieght)];
[label setText:txt1];
I see three options here.
First, make label's size big enough to hold any text. That's most simple, but does not always work well - depends on its surrounding views.
Second, Label can adapt size of the font for longer text (adjustsFontSizeToFitWidth property). This is often not desirable, different fonts in elements might look ugly.
Last option is to programmatically resize the label according to its currently holding text. To calculate the size required to hold the text with current font use something like this:
CGSize textSize = [[someLabel text] sizeWithFont:[someLabel font] forWidth:someLabel.bounds.size.width lineBreakMode:UILineBreakModeWordWrap];
If you set your font and its size already and if you have your frame defined, try using the following for these two common conditions:
if (label.text.length > maxCharPerLine) [label setNumberOfLines:0]; // infinite lines
else [label setNumberOfLines:1]; // one line only
// Adjust your font size to fit your desired width.
[label setAdjustsFontSizeToFitWidth:YES];
Using Auto Layout:
In the ViewController:
override func viewDidLoad() {
super.viewDidLoad()
sampleLabel.text = "Electrical Maintenance and Repair"
sampleLabel.sizeToFit()
}
As sizeWithFont is depreciated in IOS 7.0 then you below code
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if (SYSTEM_VERSION_LESS_THAN(#"7.0")) {
// code here for iOS 5.0,6.0 and so on
CGSize fontSize = [itemCat_text sizeWithFont:[UIFont fontWithName:#"Helvetica" size:12]];
} else {
// code here for iOS 7.0
fontSize = [itemCat_text sizeWithAttributes:
#{NSFontAttributeName:
[UIFont fontWithName:#"Helvetica" size:12]}];
}
Follow this.
CGSize stringsize = [yourString sizeWithFont:[UIFont systemFontOfSize:fontSize]];
[label setFrame:CGRectMake(x,y,stringsize.width,height)];
[label setText: yourString];
I'm trying to measure the visual size of a NSString that takes into account the number of lines I can render. However, sizeWithFont doesn't take into account numberOfLines property? So my layout algorithm positions everything lower than they actually need to be.
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
/// the follow code ignores numberOfLines and just tells me the size of the whole block.
// I'd like it to be aware of numberOfLines
//
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
Does anyone know how to do this using the iPhone SDK?
Instead of CGFLOAT_MAX for the max height of your text calculation, try getting the size of one line with this:
[_price.text sizeWithFont:_price.font].height
and then multiplying that by the maximum # of lines you want, then plugging that into the height of the size you are constraining yourself to. It'd probably look like this:
_price = [[UILabel alloc] init];
_price.text = myPriceValue;
_price.lineBreakMode = UILineBreakModeWordWrap;
_price.numberOfLines = 3;
_price.backgroundColor = [UIColor clearColor];
_price.textColor = TTSTYLEVAR(colorPrice);
CGFloat lineHeight = [_price.text sizeWithFont:_price.font].height;
CGSize priceSize = [_price.text sizeWithFont:_price.font
constrainedToSize:CGSizeMake(maxWidth, lineHeight * _price.numberOfLines)
lineBreakMode:UILineBreakModeWordWrap];
Don't use this if you ever set number of lines to 0 as your max height will be 0 in that case; you should use CGFLOAT_MAX then.
Use the UILabel's sizeToFit instead of sizeWithFont: to layout a multi-line UILabel, since sizeWithFont: will truncate the string (see apple docs). The following code reduces the font size of a label until the text fit into a the specified size... multiple lines of text will be used as soon as they fit into the specified height:
-(void)setFontSizeOfMultiLineLabel: (UILabel*)label
toFitSize: (CGSize) size
forMaxFontSize: (CGFloat) maxFontSize
andMinFontSize: (CGFloat) minFontSize
startCharacterWrapAtSize: (CGFloat)characterWrapSize{
CGRect constraintSize = CGRectMake(0, 0, size.width, 0);
label.frame = constraintSize;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0; // allow any number of lines
for (int i = maxFontSize; i > minFontSize; i--) {
if((i < characterWrapSize) && (label.lineBreakMode == UILineBreakModeWordWrap)){
// start over again with lineBreakeMode set to character wrap
i = maxFontSize;
label.lineBreakMode = UILineBreakModeCharacterWrap;
}
label.font = [label.font fontWithSize:i];
[label sizeToFit];
if(label.frame.size.height < size.height){
break;
}
label.frame = constraintSize;
}
}
Call this with a label that has your favorite text and font:
UILabel *label = [[UILabel alloc] initWithFrame: CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = text;
label.font = [UIFont fontWithName: #"Helvetica" size: 16];
[self setFontSizeOfMultiLineLabel: label toFitSize: CGSizeMake(200, 44) forMaxFontSize: 16 andMinFontSize: 8 startCharacterWrapAtSize: 11];
The startCharacterWrapAtSize parameter lets you choose to use characterWrap starting at the giving font size. This should save space in the case wordWrap would use really small fonts.
edit: bugfix
Instead of trying to do it in one call, do something like this (pardon the pseudocode, it's late):
NSString *s = _price.text;
UIFont *font = _price.font;
CGFloat fontSize = font.pointSize;
while (TRUE)
{
CGSize priceSize = [s sizeWithFont: font constrainedToSize:
CGSizeMake(maxWidth, fontSize) lineBreakMode: UILineBreakModeWordWrap];
if ( /* priceSize is satisfactory */ )
{
break; // Make sure this exits, eventually!!!
}
fontSize -= 1.0; // or a smaller decrement if you like
font = // new, smaller font
}
The correct answer is, of course, you need to set numberOfLines to 0, which will cause the framework to compute the result with however many lines it needs. See also this question.
Of course it doesn't take it into account, since nothing being called or passed in has that information. You're strictly working with strings, sizes, and fonts. It's the label that has the number of lines in it.
I'm not sure what exactly your problem is; are you getting a size that's too tall or too short, or what? You can find out the number of lines of text by dividing the height of the result by the height of the font, which is the value of the ascender plus the descender, I believe.