I am trying to set the height of a UITextView dynamically in textViewDidChange. I get the frame to adjust to the proper height, but when the height is greater than a threshold that I have set, then the textView should just scroll with a new line. However, the contentOffset gets messed up and it doesn't scroll down to fully include the new line.
- (void)viewDidLoad
{
[super viewDidLoad];
_textView.frame = CGRectMake(60, 100, 200, _textView.contentSize.height);
_textView.scrollEnabled = NO;
}
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(#"Offset: %f", _textView.contentOffset.y);
CGFloat height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
if (_textView.contentSize.height >= 120) {
height = 120;
_textView.scrollEnabled = YES;
_textView.contentOffset = CGPointMake(0, _textView.contentSize.height - _textView.frame.size.height);
}
CGRect frame = _textView.frame;
frame.size.height = height;
_textView.frame = frame;
}
I suppose an easier way of explaining what is happening is this. On a completely blank view controller add a UITextView in Storyboard. Then add only these three lines into textViewDidChange:
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
Every other time the textView's height increases, the text is not vertically centered. If you scroll it around though and then let go it will return to the correct position. The frame is always the correct size. It is the contentOffset that is not quite right.
I wonder if you could post some code as to what you're actually doing?
I've just tried this on 5.0 and this works.
-(void) textViewDidChange:(UITextView *)textView {
int maxHeight = 30;
CGFloat height = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping].height;
CGRect rect = textView.frame;
rect.size.height = MIN(height, maxHeight);
[textView setFrame:rect];
}
Related
So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.
I am working on an iPhone application, where i am opening an URL to my UIWebView. The problem is URL is not loading perfectly. As per my requirement, i need to add UIWebView as footer view of my table. but the problem is after getting the web view content height size from webViewDidFinishLoad method, i am setting frame of my footer view. here is my code :
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
aWebView.scrollView.scrollEnabled = NO;
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
[self setFooterView:fittingSize.height + aWebView.frame.origin.y];
}
-(void)setFooterView:(float)fittingHeight
{
bottomView.frame = CGRectMake(0, 0, 320, fittingHeight);
webViewSocial.frame = bottomView.frame;
[tableView reloadData];
}
But when i am scrolling my table view, then my UIWebView (footer view) is not scrolling completely. Can you please tell me how can i achieve my output. Thanks!
try to set like this
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = self.webView.frame;
frame.size.height = height;
frame.size.width = width;
self.webView.frame = frame;
self.tableView.tableFooterView = webView;
}
I have a UIScrollView with a number of children UITextViews arranged vertically. I want the UIScrollView to resize to fit content. So my TextViews I do
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
[textView setNeedsLayout];
}
And for the scrollview I do
-(void)resizeScrollViewToFitContent
{
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
}
I call [self resizeScrollViewToFitContent] inside viewDidAppear. Any ideas why this setup is not working?
I am not sure how is the arrangement of your textViews inside the scrollView. I assume that they are arranged vertically right next to each other. I have created a sample project with a scrollView and 3 textViews and it works well with the following code:-
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGFloat height = self.textView1.frame.size.height +self.textView2.frame.size.height +self.textView3.frame.size.height ;
CGSize size = CGSizeMake(self.scrollView.frame.size.width, height);
self.scrollView.contentSize = size;
}
From my own project, the content Height of the scrollview is the combination of the height for all 3 textviews. It might be different in your project.
You shouldn't be doing the union of the contentFrame and view.frame. Try the following:
-(void)resizeScrollViewToFitContent
{
UIScrollView* scrollView;
CGSize contentSize = CGSizeZero;
for (UIView* subview in scrollView.subviews)
{
CGFloat subviewRight = CGRectGetMaxX(subview.frame);
if (subviewRight > contentSize.width)
{
contentSize.width = subviewRight;
}
CGFloat subviewBottom = CGRectGetMaxY(subview.frame);
if (subviewBottom > contentSize.height)
{
contentSize.height = subviewBottom;
}
}
scrollView.contentSize = contentSize;
}
One small thing worth noting: a UIScrollView's scroll indicators are subviews of any UIScrollView. This has the potential to throw off the above code's accuracy, but I've found that it always works except in some very particular cases.
If you know how many subviews are in your scrollview per row then you'll want to do a little math to find the correct height.
For example, you might have a uiview with a uiimageview and uilabel, so you're subview count will be 3 times what it should be.
try
// csv = contentscrollview
NSLog(#"%d", [[csv subviews] count] / 3);
I am trying to write a custom view, which is a subclass of a UILabel
I dragged a UILabel to the storyboard, set it to the class of the given view.
I then tried to override draw in rect, only to find out that the label does not adjust its height to what i need.
After some further reading, i overrode the "sizeThatFits:" method with this
-(CGSize)sizeThatFits:(CGSize)size{
if(!self.bg){
return [super sizeThatFits:size];
}
CGFloat width = self.bg.size.width;
CGFloat height = self.bg.size.height;
CGFloat length = [self.text length];
CGFloat padding = self.paddingBetweenLetter * (length -1);
size.width = width*(length+padding);
size.height = size.height;
return size;
}
-(void)setBg:(UIImage *)bg{
_bg = bg;
[self sizeToFit];
[self setNeedsDisplay];
}
however, the height remains the same.
What am i missing ?
I need to create two text entry field one beneath the other. When user types in the upper text field, I need to increase the frame size dynamically (scrolling inside the textview is not recommended.) and the lower text entry frame should go downwards. Will this can be possible in ios? Any help will be appreciated.
You can update the frame of your textView via its contentSize like below:
-(void)updateTextViewSize
{
CGRect rect = textView.frame;
rect.size.width = textView.contentSize.width;
rect.size.height = textView.contentSize.height;
textView.frame = rect;
//lowering the textView2 below this textView
//assuming that the lower textview touches the
//bottom of the upper textView.
CGRect rect2 = textViewLower.frame;
rect2.origin.y = textView.frame.origin.y + textView.frame.size.height;
textViewLower.frame = rect2;
//update your scrollView here
//assuming textView and textViewLower are already added to the scrollView
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, textViewLower.frame.origin.y + textViewLower.frame.size.height);
}
//implement this delegate method as shown below
-(void)textViewDidChange:(UITextView*)textView
{
[self updateTextViewSize];
}
It is easy to do resizing of the UITextView to its correct height according to its content using the UITextView contentSize.
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;