Unable to control the width of UITextView - iphone

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.

Related

How to set the number of lines in uitextfield dynamically?

Actually i am trying to set the text in the textfield in next line when it becomes larger in the first line ad default. Can you please help me. my uitextfield is taken through the IB. and i am trying to write it and wants to post something like comment.
[viewFeedback setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background_Feedback.png"]]];
CGSize size = [txtFeedback.text sizeWithFont:txtFeedback.font];
CGRect f = txtFeedback.frame;
f.size.height = ceil(size.width/f.size.width)*size.height;
txtFeedback.frame = f;
txtFeedback is the textfield name here..
Well this would be a Increasing the height of the UITextfield dynamically. Please follow this or add your custom frame
In your UITextViewDelegate implementation of textViewDidChange: you need to calculate the size of the text for the specific width and adjust accordingly.
-(void)textViewDidChange:(UITextView *)textView
{
NSString *text = [textView text];
NSFont *font = [textView font];
CGFloat width = [textView frame].size.width;
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:UILineBreakModeWordWrap];
/* Adjust text view width with "size.height" */
}

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

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

Wrong calculation when computing size of NSString using sizeWithFont:constraintedToSIze:lineBreakMode

I have a UITableViewCell with a subview consisting of a UITextView. The content of the textview is dynamic, so I want to measure the exact width and height to create a cell and a textview with no need to scroll.
I have created the following method to measure the size the NSString will take up:
- (CGSize)getSizeOfString:(NSString *)str font:(NSString *)theFont fontSize:(CGFloat)theSize {
CGSize maxSize = CGSizeMake(280, CGFLOAT_MAX);
UIFont *font = [UIFont fontWithName:theFont size:theSize];
return [str sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
}
I am using it in this way:
// CellForRowAtIndexPath
.....
CGSize size = [strTool getSizeOfString:text font:TEXT_FONT fontSize:TEXT_SIZE];
CGFloat width = size.width;
CGFloat height = size.height;
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, width, height)];
[textView setBackgroundColor:[UIColor blueColor]];
textView.font = [UIFont fontWithName:TEXT_FONT size:TEXT_SIZE];
textView.editable = NO;
// Set value
textView.text = text;
[cell.contentView addSubview:textView];
[textView release];
return cell;
// heightForRowAtIndexPath
NSString *returnStr;
if (indexPath.section == SECTION_SHORT)
returnStr = self.manchet;
else
returnStr = self.newsText;
return [strTool getSizeOfString:returnStr font:TEXT_FONT fontSize:TEXT_SIZE].height;
But no matter what the calculated height is too small, which results in a nasty scroll inside the cellView. It is worth mentioning that almost all texts contain linebreaks (\n).
What am I doing wrong here?
Thanks
I think your ** heightForRowAtIndexPath** should look something like this:
NSString *returnStr;
if (indexPath.section == SECTION_SHORT) returnStr = self.manchet;
else returnStr = self.newsText;
return [strTool getSizeOfString:returnStr font:TEXT_FONT
fontSize:TEXT_SIZE].height+10.0f*2.0f;
And also you should take into account what you made offset for text field 15px along X (which means what width for sizeWithFont should be smaller on 15px) and 10px from top, i suggest use same value for bottom to make it look centered.
contentView have margins too, take all this into account.
UITextView is need a little bigger space than the size you calculated. since it has a top margin and bottom margin, I'm not sure how much is that. If you want perfect fit, try
[NSString drawInRect:withFont:]
or just make a little more space for UITextView, you can find the extra space by trying.

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.