Dynamically shrinking textfield's frame to text inputted - ios

I have a textview that is created dynamically during an application's run time, with text inputted by user. How do I go about setting up the frame of the textview such that the frame is just about the size of the text? What I'm looking for is similar to how bubbles shrink and expand in, say, iMessages.
I'm using the following piece of code
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
to adjust the height, but I'm not sure how to adjust both the height and width at the same time, given they both depend on user input.

[_textView sizeToFit];
Hope it helps.

Related

Dynamically resize label based on lines of text

I am finding it surprisingly hard to resize a label containing newlines based on the quantity of lines and text. It displays fine in a large enough textview. However, I'd like the economy of sizing the label--or I'd be happy with resizing a textview--exactly.
This is the code I am using from an answer on SO but it is having no effect on the size of the label. Would appreciate any suggestions on how to make this work:
NSString *list = self.list.list;
// use font information from the UILabel to calculate the size
UITextView *view=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 280, 10)];
//make random size view
view.text=list;
CGSize size=[view sizeThatFits:CGSizeMake(280, CGFLOAT_MAX)];
// create a frame that is filled with the UILabel frame data
CGRect newFrame = _listLabel.frame;
// resizing the frame to calculated size
newFrame.size.height = size.height;
// put calculated frame into UILabel frame
_listLabel.frame = newFrame;
Why are you setting the frame of your label with reference of a newly created UITextView, it will create a useless object in your memory, to set the label frame according to your text just use this 2 line of code
lbl.numberOfLines=0;
[lbl sizeToFit];
It will make the label as large as your text.
You really should use autolayout.
Just constrain the label where you need and let UIKit do it's job.
Here an example:
I set a top space and a leading margin constraints
Then I added a width constraint and then I added some more text
As you can see the label resized itself as it knows how much text it has inside and how much space it occupies.

How to make UITextView so high that it exactly fits the text content on iOS7?

I have a UITextView that's embedded into a UIScrollView. I would like the text view to not scroll and be exactly as high as required to show all of the text.
So the width is fixed and I set the content insets to indent the text a bit.
How do I get the correct height? I tried to set the frame's height to the content height but still it scrolls.
This should do the trick:
-(void)resizeTextView
{
CGRect frame = _textView.frame;
frame.size.height = [_textView sizeThatFits:CGSizeMake(frame.size.width, INFINITY)].height;
_textView.frame = frame;
}

Changing textView height depending of text it contain

i have detail view controller that shows up after user click on cell, it contain UIImageView, textview that have some text. Sometimes it have several rows of text, sometimes a lot of text. What i want to, is change textView frame height dynamically, depending of amount of text it contain.
That is what i have tried:
-(void)configureTextView{
CGRect frame = self.myTextView.frame;
frame.size.height = self.myTextView.contentSize.height;
self.myTextView.frame = frame;
}
But it still show me textView that not fit whole rows of text (user can actually scroll it up and down, feature that i don't want to). How to change textView frame height for height, that actually fit whole text? And how to measure final height size of that textView?
Any advice would be appreciated, thanks!
You can try [self.myTextView sizeToFit]; and then see if it updates it for you.
- (CGFloat)heightForTextView:(UITextView*)textView containingString:(NSString*)string
{
float height = [self.model boundingRectWithSize:CGSizeMake(widthOfTextView, 20000000)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont fontWithName:#"Helvetica Neue" size:15]}
context:nil].size.height + 16;
return height;
}

How to increase the height of UITextView when Text is more than Height

I have a UITextView and when i am entering data into that after 5-6 lines , the data is scrolling up and it cannot be seen. Is there any property that i can use to increase the height of UITextView as the text are entered more than height.
Pls suggest guys.
Use this code to make the height of the UITextView be the same as the height of the content inside it.
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

UITextView text cut off when there is a large amount of text

I have a UITableViewCell subclass that contains a UITextView where scrolling is turned off. I set its frame like this, in my table view cell layoutSubviews method:
CGRect frame = self.bodyTextView.frame;
CGSize size = self.bodyTextView.contentSize;
frame.size = size;
self.bodyTextView.frame = frame;
This has been working fine for some time, but I've noticed that in a case where I have a particularly large amount of text, the text is getting cut off. I've set the text view frame background color to orange so I could verify that the frame was being set correctly. Here is an example (I am only showing the bottom portion of the text view):
The frame is the correct size based on the text (in this case 1019 points), but the text stops before the bottom of the text view. I have also seen the text get cut off part way through a line, (ie the text of the last visible line of text is cut off half way through horizontally). Does anyone have an idea what is happening here?
A few other points:
The text views work well for all my table view cells with shorter content.
If I increase the amount of text in the case shown above, the text view height increases, but the text still gets cut off at the same place.
According to this and similar answers, the problem could be that "the correct contentSize is only available after the UITextView has been added to the view ... Prior to that it is equal to frame.size"
I'd suggest you to calculate the height in a different way, like -sizeWithFont: or -sizeTofit
Use this to get the textSize
// find the text width; so that btn width can be calculate
CGSize textSize = [#"YOUR TEXT" sizeWithFont:[UIFont fontWithName:#"Arial" size:20.0]
constrainedToSize:CGSizeMake(320.0f, 99999.0f)
lineBreakMode:UILineBreakModeWordWrap];
and then set height using this as :
CGRect frame = self.bodyTextView.frame;
frame.size = textSize;
self.bodyTextView.frame = frame;
Hope it helps you.
I had the same issue and resolved it working on the textView padding (in swift5):
yourTextView.textContainerInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
Hope it can help :)

Resources