Changing textView height depending of text it contain - ios

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

Related

Make UILabel height zero if label empty/storyboard

I have a lot of labels stacked up for a contact info screen. If any of the labels are empty, I would love to zero out their height and have them occupy no space to avoid empty space on the screen. However, I have created the screen in storyboard and the labels have been assigned heights and y values.
Here is code I've been trying to use to alter label height but cannot get it to work. Perhaps it is designed to work only for labels created programmatically and the storyboard settings override what I am doing here.
NSString *string = #"some text";
CGSize maximumLabelSize = CGSizeMake(280,1000);
 
// use font information from the UILabel to calculate the size
CGSize expectedLabelSize = [string sizeWithFont:myLabel.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
 
// create a frame that is filled with the UILabel frame data
CGRect newFrame = myLabel.frame;
// resizing the frame to calculated size
newFrame.size.height = expectedLabelSize.height;
// put calculated frame into UILabel frame
myLabel.frame = newFrame;
Is there a way to get the height of labels created in storyboard to zero out if they are empty?
you can override the following method of the view containing your label and do something like that:
-(void) layoutSubviews {
myLabel.frame = CGRectZero;
}
This method is in order to allow to layout the subviews or you view. Now I don't know If you have used contraints to define the height of your label. If so you should change in the same way the height of your label. Note the in the above example I used CGRectZero.

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

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;

Dynamically shrinking textfield's frame to text inputted

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.

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