Increase the height of a UITextView - iphone

scrollView = [[UIScrollView alloc] initWithFrame:
[[UIScreen mainScreen] applicationFrame]];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 100, 200, 250)];
textView .text =#"long text ";
I will be getting some data of unknown length. to be added to the UITextView . Sometimes the height of the content of the text might exceed the height of the UITextView which is 250 (shown above).
How can i increase the height of the UITextView based on the text i receive ? I would appreciate a sample code?
note: according to my code i am defining the width and height of the UITextView before adding the text to it.

You can use NSString's sizeWithFont:constrainedToSize:lineBreakMode: to get a CGSize struct telling you how big the text will be.
Usage:
CGSize size = [mystring sizeWithFont:[UIfont systemFontOfSize:15]
constrainedToSize:CGSizeMake(200, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
[myTextView setFrame:CGRectMake(25,100,200,size.height)];

Related

set the width of a UIText view based on the content size property

TextView.contentSize.width does not work to set the UITextView's .frame.size.width.
[TextView setFrame:CGRectMake(TextView.frame.origin.x, TextView.frame.origin.y, TextView.contentSize.width, TextView.contentSize.height)];
Setting the UITextView's frame height to the contentSize.height property works to make the view's frame scale to the proper size for the current vertical size of the content. For some reason, the width of the view's frame does not respond in the same way. It just remains the same size regardless of the amount of text input.
When I log the contentsize of the UITextView dynamically, as I am typing in text to the view, the height property changes (at each line break), while the width does not. Makes me wonder what the width property is doing, what's it for.
Try this way..
UIFont *font = [UIFont fontWithName:#"Enriqueta" size:15];
NSString *text = #"Your text";
CGSize frameSize = [text sizeWithFont:font];
CGRect originalFrame = textViewA1.frame;
textViewA1.frame = CGRectMake(CGRectGetMinX(originalFrame), CGRectGetMinY(originalFrame), frameSize.width, frameSize.height);
As depending upon width of UITextView it should be like this:
UIFont *myFont = [UIFont boldSystemFontOfSize:15.0]; //your font specification here
NSString *strText = yourTextView.text;
CGSize strsize = [strText sizeWithFont:myFont
forWidth:yourTextView.frame.size.width
lineBreakMode:UILineBreakModeWordWrap]; //get string size
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x,yourTextView.frame.origin.y,strsize.width,strsize.height+10); //change accordingly

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

EDIT: UITextView Label is Cut in Half (Horizontally)

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 :)

UILabel's sizeToFit/sizeThatFits ignore the numberoflines property

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.

How to adjust and make the width of a UILabel to fit the text size?

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];