Dynamic UITextView in a dynamic UIScrollView - iOS - iphone

I'm facing a problem which takes me too long to solve.
I built a view, which contains a ScrollView. in the ScrollView, there's a view, which contains an image, and a UITextView. the textview should be in a dynamic height, with scrolling disabled. the textview gets all the text, but cut it off, and shows only the text that fits the height. in addition, the ScrollView doesn't changes.
- (void)viewDidLoad
....
//sets the text to the textview
[self.contentArticle setText:[NSString stringWithString:xmlParser.articleContent]];
//configures the scrollView
[self configureScrollView];
....
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect frame = self.contentView.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.scrollView.frame = frame;
[self.contentView sizeToFit];
[self.scrollView sizeToFit];
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
I did so many changes, and im not sure what is going on in there anymore!...
help would be sooo nice :)
UPDATE-
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
[self.scrollView setContentSize:textViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
}

Try
- (void)configureScrollView{
self.contentView.autoresizingMask = UIViewAutoresizingNone;
self.contentArticle.autoresizingMask = UIViewAutoresizingNone;
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.height = textViewFrame.origin.y+textViewFrame.size.height;
self.contentView.frame = contentViewFrame;
[self.scrollView setContentSize:contentViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}
Source code

had same issue , tried to find solution for many days and finally i got it working.
I have a textview inside a scrollview.
disabled autolayout from storyboards
ive added the textview to scrollview with addsubview
and finally set the content of the scrollview, to the textview frame height.
Below my code:
_textView.text = stripped; //(some string ,yours: contentArticle)
[_textView sizeToFit];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
_textView.frame = frame;
[self.scrollView addSubview:_textView];
[self.scrollView setContentSize:CGSizeMake(320,frame.size.height)];
Hope it helps!

You need to set self.contentView height first Use this code:
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
CGRect frame = self.contentArticle.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.contentArticle.frame = frame;
[self.scrollView addSubview:self.contentView];
frame = self.contentView.frame;
frame.size.height += self.contentArticle.frame.height;
self.contentView.frame = frame;
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}

Related

iOS: Adding UILabel to zoomed UIScrollview was blurred

I am facing one strange issue when adding UILabel to zoomed UIScrollview problem.
I am adding two views to zoomed UIScrollview
1. UIImageView
2. UILabelView
Here the code am using to add UIImageView on UIScrollview
float recentZoomScaleValue = 4.5;
CGRect rect = CGRectMake(x, y, 150, 150);
UIImageView *signatureImage = [[UIImageView alloc]initWithFrame:rect];
[signatureImage setImage:image];
[signatureImage setFrame:rect];
//resize the frame to avoid the auto zoom
CGRect frame = signatureImage.frame;
frame.size.width /= recentZoomScaleValue;
frame.size.height /= recentZoomScaleValue;
[signatureImage setFrame:frame];
[self addSubView:signatureImage];
[self.scrollview addSubView:signatureImage];
Here the code am using to add UILabel on UIScrollview
float recentZoomScaleValue = 4.5;
txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, self.fontSize)];
[txtLabel setText:#"loganathan is a good boy"];
[txtLabel setFont:[UIFont fontWithName:txtLabel.font.fontName size:self.fontSize]];
[txtLabel setTextColor:[UIColor redColor]];
[txtLabel setBackgroundColor:[UIColor clearColor]];
[txtLabel setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
CGRect frame = signatureImage.frame;
frame.size.width /= recentZoomScaleValue;
frame.size.height /= recentZoomScaleValue;
[txtLabel setFrame:frame];
[self addSubview:txtLabel];
But the problem is when ever i tried to add UILabel view it added with auto zoomed. Since the label text was blurred. I do not know why and what is the problem. Shall i use CATextLayer instead of UILabel?. Any help that might be appreciated.
Thanks
you have to set the contentsScale to the right value when ever the zoomScale changes. Or initial to the current zoomScale:
Example for dynamic zooming:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[yourLabel.layer setContentsScale:scale*[[UIScreen mainScreen] scale]];
[yourLabel.layer setNeedsDisplay];
}

UITextView will not resize

I have tried resizing a UITextView (inside a tableViewCell) to the content size. but it will not change its height at all. I have even changed the height of the UITableViewCell. What could be wrong?
- (void) setTextViewContents: (NSString*) string
{
[textView setText:string];
CGRect frame2 = self.frame;
frame2.size.height = 10000;
self.frame = frame2;
/* resize the view */
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height+60;
textView.frame = frame;
The string does appear on the view but the size does not change.
Try calling the -[UIView sizeToFit] method.

iOS - UIScrollView is not working (it doesn't scroll at all - the image stays fixed)

I would like to display an image (width: 320 pixels, height: 1250 pixels) in an image view.
When I compile the application I get no scrolling at all. The view stays fixed.
What I did:
Added an UIScrollView via Interface Builder to my view.
Added an UIImageView via Interface Builder to my view.
Verified that UIImageView is below UIScrollView in Interface Builder.
Set the size of the UIScrollView with ViewDidLoad.
How do I do this?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.contentSize = CGSizeMake(320, 1250);
}
Screenshots:
ImageView:
ScrollView:
I just have done the same task..
Try this one.....
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 50, 50);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 70;
}
Also set this one....
imagesName = [[NSArray alloc]initWithObjects:#"image1.jpg",#"image2.jpg",#"image3.jpg",#"image4.jpg",#"image5.jpg",#"image6.png",#"image7.png",#"image9.png",nil];
images = [[NSMutableArray alloc]init];
So for me the problem was that setting the content size didn't work in viewDidLoad(). I tried everything and I didn't understand why it wouldn't want to work, and then I tried the same stuff in viewDidAppear() and it magically worked...
From you last screenshot and from your comments it looks like your scrollView is way to big.
The scrollview must be visible on screen completely. For example a full screen UIScrollView on iPhone would have a size of 320 x 460.
If the scrollview is the same size as its content you can't scroll.
The greenish rectangle shows the size of your scrollview, the pinkish the size of your content (your image):
Xcode 11+, Swift 5
You can find the complete solution here.
I came across this same issue on iOS6 and the solution was to programmatically adjust the ContentSize.
So I will just quote from Raja (above) to show this:
CGSize scrollViewContentSize = CGSizeMake(320, 400);
[self.scrollView setContentSize:scrollViewContentSize];
NOTE: I was not having this issue on iOS5.. seems like iOS6 decided to do alot of prank just like the rotation/orientation saga
Since Xcode 5 it does not work like before. Also scrolling to the end of a scrollable text field makes problems. There are also differences between doing it on iPhone or iPad. On iPhone it worked without delayed timer.
This worked for me:
- (void)viewDidLoad
{
[super viewDidLoad];
NSTimer *timerforScrollView;
timerforScrollView =[NSTimer scheduledTimerWithTimeInterval:0.1
target:self selector:#selector(forScrollView)userInfo:nil repeats:NO];
}
- (void) forScrollView {
[scrollviewPad setScrollEnabled:YES];
[scrollviewPad setContentSize:CGSizeMake(768, 1015)]; // must be greater then the size in Storyboard
}
I found I had a similar problem but none of the above code worked. The issue was due to autolayout. I found that if I turned off autolayout by going to the storyboard clicking on Utilities -> File Inspector and unchecked Use Autolayout the scrolling did work (I also set scroll.contentSize = ...).
Sometimes autoLayout checkbox is ticked in the xib. That also creates this issue in xcode 5. Which makes the UIScrollView scrolling off.
Don't forget to add the category protocol to the interface, like this
#interface MyViewController : <UIScrollViewDelegate>
If you don't, you will not be able to set the scroll view delegate to self
(i.e. [MyScrollView setDelegate:self];)
If you do this, it should work.
My code is:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[contentScrollView setDelegate:self];
[contentScrollView setScrollEnabled:YES];
contentScrollView.contentSize = CGSizeMake(310, 500);
contentScrollView.frame = CGRectMake(5, 188, 310, 193);
}
Did you assign the scroll's view delegate? Always remember these:
[self.scrollView setDelegate:self];
[self.scrollView setScrollEnabled:YES];
The image view has to be a subview (so inside AND below) of the scrollview. From your description it seems they are paralell
You forgot one line. Add this to your view load function:
[scrollView setScrollEnabled:YES];
You could try disabling AutoLayout. In XCode 5 I tested all the above answers and I could only scroll it by disabling autolayout and activating all autosizing masks under the Size Inspector. The following code was used too:
self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;
self.scrollView.frame = self.view.frame;
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 400);
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[self.view addSubview:self.scrollView];
CGSize scrollViewContentSize = CGSizeMake(320, 400);
[self.scrollView setContentSize:scrollViewContentSize];
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
Assuming that scrollView is a subview of view and fills it entirely you can use the following in viewDidLoad:
[scrollView setContentSize: CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
I had a UIScrollView that was not scrolling and this allowed it to scroll.
Just add code
scrollView.contentSize = CGSizeMake(WIDTH,HEIGHT);
to method -(void)viewDidLayoutSubviews.
For more information checkout Stanford CS193p Lecture No 8 to understand View Controller Life cycle.
I had the same issue and was looking for the answer in this thread. I tried all the stuff, but nothing works. Then I found this:
.
You just need to deselect "Use Auto Layout" in File Inspector of your ViewController. Ta-Da, it works immediately for me. Enjoy.

How to slide a UIView right above a UITabBar

I have a UIView that I want to slide from behind a UITabBar to be position right on top of it.
This doesn't work. My view does not appear.
- (void)showNotificationBar
{
CGRect frame = CGRectMake(0, 500, 320, 32);
frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame) - frame.size.height;
notificationBar.frame = frame;
[self.parentViewController.tabBarController.tabBar.superview insertSubview:notificationBar
belowSubview:self.parentViewController.tabBarController.tabBar];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = notificationBar.frame;
frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame);
notificationBar.frame = frame;
}];
}
Initialize frame.origin.y like this:
frame.origin.y = self.tabBarController.tabBar.frame.origin.y;
In the animations block, set it like this:
frame.origin.y -= frame.size.height;
If you want it to show in every view, you could do to things: either show it at the bottom of every view or show it in the application's window. I personally like this second approach better because it helps avoiding duplicate code:
CGFloat notificationBarHeight = 40.0f;
UIView *notificationBar = [[UILabel alloc]initWithFrame:CGRectMake(0, self.tabBarController.tabBar.frame.origin.y - notificationBarHeight, 320, notificationBarHeight)];
[self.window insertSubview:notificationBar atIndex:[[self.window subviews]count]];
[self.view insertSubview:notificationView atIndex:1]; worked for me...

Resizing UITextView

I have a UITextView added on my UIView. The textview added is not editable, it is just to display some data. The data displayed in the textview is dynamic. Thats is the number of lines is not fixed. It may vary. So if the number of line increases, the size of the textview also needs to be increased. I have no clue how to do this. Please give me some ideas.
UPDATE:
Here's what I'm doing:
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
baseView.backgroundColor = [UIColor grayColor];
[window addSubview:baseView];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 100, 30)];
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
textView.text = #"asdf askjalskjalksjlakjslkasj";
[textView sizeToFit];
[baseView addSubview:textView];
There is an answer posted at How do I size a UITextView to its content?
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
or better(taking into account contentInset thanks to kpower's comment)
CGRect frame = _textView.frame;
UIEdgeInsets inset = textView.contentInset;
frame.size.height = _textView.contentSize.height + inset.top + inset.bottom;
_textView.frame = frame;
note: If you are going to reference a property of an object many times(e.g. frame or contentInset) it's better to assign it to a local variable so you don't trigger extra method calls(_textView.frame/[_textView frame] are method calls). If you are calling this code a lot(100000s of times) then this will be noticeably slower(a dozen or so method calls is insignificant).
However... if you want to do this in one line without extra variables it would be
_textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, _textView.frame.size.width, _textView.contentSize.height + _textView.contentInset.top + _textView.contentInset.bottom);
at the expense of 5 extra method calls.
You can use setFrame: or sizeToFit.
UPDATE:
I use sizeToFit with UILabel, and it works just fine, but UITextView is a subclass of UIScrollView, so I can understand why sizeToFit doesn't produce the desired result.
You can still calculate the text height and use setFrame, but you might want to take advantage of UITextView's scrollbars if the text is too long.
Here's how you get the text height:
#define MAX_HEIGHT 2000
NSString *foo = #"Lorem ipsum dolor sit amet.";
CGSize size = [foo sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(100, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
and then you can use this with your UITextView:
[textView setFont:[UIFont systemFontOfSize:14]];
[textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];
or you can do the height calculation first and avoid the setFrame line:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 30, 100, size.height + 10)];
sizeToFit Does Work
If you call sizeToFit after you set the text the first time it resizes. So after the first time you set it subsequent calls to set text will result in no change in size. Even if you call sizeToFit.
However, you can force it to resize like this:
Set the text.
Change the textView frame height to be CGFLOAT_MAX.
Call sizeToFit.
textView.contentSize.height in the textViewDidChange can only resize after text actually grows. For best visual result better to resize beforehand. After several hours I've figured out how to make it the same perfectly as in Instagram (it has the best algorithm among all BTW)
Initialize with this:
// Input
_inputBackgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, size.height - _InputBarHeight, size.width, _InputBarHeight)];
_inputBackgroundView.autoresizingMask = UIViewAutoresizingNone;
_inputBackgroundView.contentMode = UIViewContentModeScaleToFill;
_inputBackgroundView.userInteractionEnabled = YES;
[self addSubview:_inputBackgroundView];
[_inputBackgroundView release];
[_inputBackgroundView setImage:[[UIImage imageNamed:#"Footer_BG.png"] stretchableImageWithLeftCapWidth:80 topCapHeight:25]];
// Text field
_textField = [[UITextView alloc] initWithFrame:CGRectMake(70.0f, 0, 185, 0)];
_textField.backgroundColor = [UIColor clearColor];
_textField.delegate = self;
_textField.contentInset = UIEdgeInsetsMake(-4, -2, -4, 0);
_textField.showsVerticalScrollIndicator = NO;
_textField.showsHorizontalScrollIndicator = NO;
_textField.font = [UIFont systemFontOfSize:15.0f];
[_inputBackgroundView addSubview:_textField];
[_textField release];
[self adjustTextInputHeightForText:#""];
Fill UITextView delegate methods:
- (void) textViewDidBeginEditing:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
- (void) textViewDidEndEditing:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
- (BOOL) textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
if ([text isEqualToString:#"\n"])
{
[self performSelector:#selector(inputComplete:) withObject:nil afterDelay:.1];
return NO;
}
else if (text.length > 0)
{
[self adjustTextInputHeightForText:[NSString stringWithFormat:#"%#%#", _textField.text, text]];
}
return YES;
}
- (void) textViewDidChange:(UITextView*)textView {
[self adjustTextInputHeightForText:_textField.text];
}
And the trick is...
- (void) adjustTextInputHeightForText:(NSString*)text {
int h1 = [text sizeWithFont:_textField.font].height;
int h2 = [text sizeWithFont:_textField.font constrainedToSize:CGSizeMake(_textField.frame.size.width - 16, 170.0f) lineBreakMode:UILineBreakModeWordWrap].height;
[UIView animateWithDuration:.1f animations:^
{
if (h2 == h1)
{
_inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - _InputBarHeight, self.frame.size.width, _InputBarHeight);
}
else
{
CGSize size = CGSizeMake(_textField.frame.size.width, h2 + 24);
_inputBackgroundView.frame = CGRectMake(0.0f, self.frame.size.height - size.height, self.frame.size.width, size.height);
}
CGRect r = _textField.frame;
r.origin.y = 12;
r.size.height = _inputBackgroundView.frame.size.height - 18;
_textField.frame = r;
} completion:^(BOOL finished)
{
//
}];
}
This works perfectly for me:
#define MAX_HEIGHT 2000
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(100, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
[textview setFont:[UIFont systemFontOfSize:14]];
[textview setFrame:CGRectMake(45, 6, 100, size.height + 10)];
textview.text = text;
Do the following:
_textView.text = someText;
[_textView sizeToFit];
_textView.frame.height = _textView.contentSize.height;
Addressing the similar issue I just created a an auto-layout based light-weight UITextView subclass which automatically grows and shrinks based on the size of user input and can be constrained by maximal and minimal height - all without a single line of code.
https://github.com/MatejBalantic/MBAutoGrowingTextView
The answer given by #Gabe doesn't work in iOS7.1 seemingly until after viewDidAppear. See my tests below.
UPDATE: Actually, the situation is even more complicated. If you assign textView.text in the resizeTheTextView method, in iOS7, the resizing amounts to allowing for only a single line of text. Seriously odd.
UPDATE2: See also UITextView content size different in iOS7
UPDATE3: See my code at the very bottom for what I'm using now. Seems to do the job.
#import "ViewController.h"
#interface ViewController ()
{
UITextView *textView;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 1)];
[self.view addSubview:textView];
CALayer *layer = textView.layer;
layer.borderColor = [UIColor blackColor].CGColor;
layer.borderWidth = 1;
textView.text = #"hello world\n\n";
// Calling the method directly, after the view is rendered, i.e., after viewDidAppear, works on both iOS6.1 and iOS7.1
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Change size" forState:UIControlStateNormal];
[button addTarget:self action:#selector(resizeTheTextView) forControlEvents:UIControlEventTouchUpInside];
[button sizeToFit];
CGRect frame = button.frame;
frame.origin.y = 400;
button.frame = frame;
[self.view addSubview:button];
// Works on iOS6.1, but does not work on iOS7.1
//[self resizeTheTextView];
}
- (void) viewWillAppear:(BOOL)animated
{
// Does not work on iOS7.1, but does work on iOS6.1
//[self resizeTheTextView];
}
- (void) viewDidAppear:(BOOL)animated
{
// Does work on iOS6.1 and iOS7.1
//[self resizeTheTextView];
}
- (void) resizeTheTextView
{
NSLog(#"textView.frame.size.height: %f", textView.frame.size.height);
NSLog(#"textView.contentSize.height: %f", textView.contentSize.height);
// 5) From https://stackoverflow.com/questions/728704/resizing-uitextview
CGRect frame = textView.frame;
UIEdgeInsets inset = textView.contentInset;
frame.size.height = textView.contentSize.height + inset.top + inset.bottom;
textView.frame = frame;
NSLog(#"inset.top: %f, inset.bottom: %f", inset.top, inset.bottom);
NSLog(#"textView.frame.size.height: %f", textView.frame.size.height);
NSLog(#"textView.contentSize.height: %f", textView.contentSize.height);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
UPDATE3:
if ([[UIDevice currentDevice] majorVersionNumber] < 7.0) {
CGRect frame = _abstractTextView.frame;
UIEdgeInsets inset = _abstractTextView.contentInset;
frame.size.height = _abstractTextView.contentSize.height + inset.top + inset.bottom;
_abstractTextView.frame = frame;
}
else {
CGSize textViewSize = [_abstractTextView sizeThatFits:CGSizeMake(_abstractTextView.frame.size.width, FLT_MAX)];
_abstractTextView.frameHeight = textViewSize.height;
}
After you add the UITextView to its parent if you set a Content Mode on it then it seems to resize itself automatically.
This means you don't need to work out the height manually and apply a height contraint. It just seems to work!! Tested in iOS7 and iOS8 on iPad.
e.g.
--
textView.contentMode = UIViewContentMode.Center;
--
If anyone can explain why this works it would be much appreciated.. I found it by accident when messing with options in interface builder.
Just set scrollEnabled to NO, or uncheck Scrolling Enabled in the Scroll View section in IB and the UITextView will self-size.