By default, the UITextView's contentView becomes scrollable when there is too much text to fit into the textview based on it's height.
I'd like to disable this and instead, update the height of the UITextView to fit in the text. The reason I'm doing this is because I'm adding the UITextView as a subview of a UIScrollView which should handle the scrolling, much like in the native Mail app (when you enter text, the whole view scrolls up, not just the textview.
Anyone got some ideas / has run into the same problem before?
It is very simply done like this:
CGRect frame = textView.frame;
frame.size = textView.contentSize;
textView.frame = frame;
This should set the height of the textview to appropriately fit all of its content.
Few little changes:
-(void)textViewDidChange:(UITextView *)textView {
CGFloat fontHeight = (textView.font.ascender - textView.font.descender) + 1;
CGRect newTextFrame = textView.frame;
newTextFrame.size = textView.contentSize;
newTextFrame.size.height = newTextFrame.size.height + fontHeight;
textView.frame = newTextFrame;
}
Adding the font height gives room for the autocorrection box when you spell something incorrectly.
The UITextView should also be set to not scroll:
[aTextView setScrollEnabled:NO];
Related
Hi I have UITextView that I want it to make its height right above the keyboard. I am making a notes app and I need to bring the UITextView up so it scroll easily.Here is the code:
CGRect keyboardBounds = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect frame = self.notesTextView.frame;
CGFloat height = self.notesTextView.frame.size.height;
height = frame.size.height;
frame.size.height -= keyboardBounds.size.height;
self.notesTextView.frame = frame;
However the screen turns out like this:
Here's the answer you need: https://stackoverflow.com/a/1127025/189804
Doing this right is hard but essential. Don't forget to use all the information in the info dictionary passed to the keyboard notification, You need to read the height of the emerging keyboard rather than hard-coding a value, you need to get the animation duration so you can rearrange your views with the same animation duration and you also need to respond to events while editing - imagine that a user begins editing, then decides to connect a bluetooth keyboard to make the editing task easier, or they begin editing with the keyboard connected and then the keyboard battery quits...
You need to resize the scroll view. Don't touch the textview's frame ever (unless you make a subclass).
CGRect frame = self.textView.enclosingScrollView.frame;
frame.size.height = 42;
self.textView.enclosingScrollView.frame = newFrame;
I have a text view inside a scroll view, I want to let the height of text view be dynamic as its content, because I don't want to scroll the text view alone, In fact I want to scroll the text view with the scroll view together.
In fact, I confused with the right menu of the image above, about the constraints height equals, less than, bigger than,...
I don't want to let the height stable, I want it dynamic, in my code:
CGRect frame = self.detailTextView.frame;
frame.size.height = self.detailTextView.contentSize.height;
self.detailTextView.frame = frame;
scroll.contentSize = CGSizeMake(scroll.contentSize.width,
200 + self.detailTextView.frame.size.height);
[self.detailTextView setFrame:frame];
which detailTextView is the name of text view, and scroll is the name of the scroll view.
But it doesn't work properly, what shall I do please help!
Note: Is there any relation between this issue and the checkbox of (use AutoLayout), because it is checked in my application
What the problem is? I have seen only one issue that you need to disable the scrolling of youtTextView.
CarinaM
Use following method for getting dynamic height of textfield as per text.
-(CGFloat) GetHeightFoText:(NSString *)aStrTxt FoWidth:(int)aIntWidth ForFontSize: (int)aIntFntSize
{
CGSize maximumLabelSize = CGSizeMake(aIntWidth,9999);
CGSize expectedLabelSize = [text sizeWithFont:[UIFont systemFontOfSize:aIntFntSize]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
return expectedLabelSize.height;
}
I need a dynamically resizing UITextView but the right margin encroaches towards the left alarmingly after numerous resizes so that a very narrow strip of text is shown with lots of white space in the text view. This can be reproduced by a simple setup with just a UITextView and UISlider. A simple sample setup that produces this behavior is UISlider with value range from 0-200, a UITextView of 320 width and this code:
- (IBAction)sliderValueChanged:(UISlider *)slider {
textView.frame = CGRectMake(0, 0, 320 - slider.value, 300);
}
Some things I've tried are tinkering with the autoResizingMask, contentMode, contentOffset, and sizeToFit but none of them work. How can this weird behavior be avoided, or is it a bug?
Subclass UITextView and override layoutSubViews to set the frame to CGRectZero and then back to original frame size. It's not elegant but it works.
-(void) layoutSubviews
{
CGRect rect = [self frame];
[self setFrame:CGRectZero];
[self setFrame:rect];
}
How can you make a UITextView expand with the text that is inside of it?
you could try this...
UITextView *textView; // your UITextView
NSString *text; // the text that you want to place in the UITextView
UIFont *textViewFont; // the font that you are using for your UITextView
CGSize size = {255,2000.0f}; //The default width, and max height you want to use
CGSize newSize = [text sizeWithFont:textViewFont
constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
textView.frame = CGRectMake(0,0,newSize.width, newSize.height);
You can try this..
CGRect frame = textView.frame;
frame.size.height = [textView contentSize].height;
textView.frame = frame;
Are you using UITextView inside UITableView?
Let's suppose you are using UITextView inside UITableView. In order to make UITextView height dynamic(As per the content inside it) we need to take care of following things:-
Make sure UITextView constraints are properly given. e.g: I had used XIB for tableview cell and inside cell I had a UITextView. So, I will give constraint to UITextView 0 to all four sides e.g: Top, bottom, leading, trailing. (Depends on your requirements).
Make sure UITextView attribute, autoscroll is disabled. It should be off.
Make sure TableView cell height is UITableView.automaticDimension
Now inside your 'textViewDidChange' delegate method, Just add this below mentioned code:
UIView.setAnimationsEnabled(false)
tableView?.beginUpdates()
tableView?.endUpdates()
UIView.setAnimationsEnabled(true)
Now your textView will auto expand, while you are typing init.
I already did several searches on Stack Overflow and Google, but I couldn't find a solution to my problem.
I have a Detail View for a product that includes a UIScrollView and a few subviews (UIImageView, UILabel, UITextView). You can find an example here.
First I wanna autoresize the UITextView (not the text itself!) to the corresponding height. Then I wanna autoresize the entire UIScrollView so that it fits the height of my UITextView and the elements above it. I've tried the following code:
myUITextView.frame = CGRectMake(2.0, 98.0, 316.0, self.view.bounds.size.height);
[scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
scrollView.contentSize = CGSizeMake(320.0, 98.0 + [myUITextView frame].size.height);
[self.view addSubview:scrollView];
98.0 + [myUITextView frame].size.height) because my thought was: After getting the height of myUITextView, add the height of the other subviews (98.0) to it.
Unfortunately it doesn't work very well. Depending on the content of the UIScrollView, it is too short or too long. I did some logging with the result that the height of the UITextView is always the same:
2010-01-27 14:15:45.096 myApp[1362:207] [myUITextView frame].size.height: 367.000000
Thanks!
There is actually a very easy way to do resize the UITextView to its correct height using its contentSize.
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
One thing to note is that the correct contentSize is only available after the UITextView has been added to the view with addSubview. Prior to that it is equal to frame.size
A UITextView won't automatically resize itself to it's contents (not as far as I know anyway) so you need to get the size of the text yourself and size the UIView accordingly.
The functions on this page will help - you can use them to get the width and height of a string, something like
// Get the size that the string will render at
NSString *contents = #"This is the content of your UITextView";
CGSize areaSize = [contents sizeWithFont:myView.font forWidth:myView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];
// Then set the frame of your UITextView to be the right size
myView.bounds = CGRectMake(0, 0, areaSize.width, areaSize.height);
Then, you can layout the other components around it.
Hope this helps,
S
PS Warning, the link to the docs is correct but my code example is untested, sorry :)