AutoLayout Scrolling UITextView sizeThatFits not working - objective-c

In my UIViewController, I have a non-editable attributed UITextView with scrolling enabled, and I would like to resize the height of the scrolling text to accommodate all screen size widths without having extra white space. I am using AutoLayout and set up outlets for the textview and its height constraint. In the view controller's viewDidLoad method I use the sizeThatFits method to update the textview height, but the resulting size is too small.
- (void)viewDidLoad{
[super viewDidLoad];
NSLog(#"INIT HEIGHT: %f, myTextView.frame.size.height);
CGSize sizeToFit =
[myTextView sizeThatFits: CGSizeMake(myTextView.frame.size.width, MAXFLOAT)];
myTextViewHeightConstraint.constant = sizeToFit.height;
[myTextView updateConstraints];
[myTextView layoutIfNeeded];
NSLog(#"NEW HEIGHT: %f", myTextView.frame.size.height);
}
The log indicates the height was indeed changed from my default of 4000 to 2502, but with 2502 I only see about half of my text via scrolling. I am avoiding nesting the text view in a scroll view, as one solution suggests, since this is discouraged in documentation. What am I missing? Thanks in advance.

I found that the frame of a UITextView will automatically size to its content, so dynamically setting it is not necessary. sizeThatFits is a UIView method and doesn't modify the size of the text content, but rather the size of the window that the text appears in. The only code I needed was to scroll the text to the top:
[myTextView scrollRectToVisible: CGRectMake(0,0,1,1) animated:NO];
This, as well as any Autolayout constraint updates shouldn't be made in viewDidLoad, but rather in viewDidLayoutSubviews or viewWillAppear. Best placement could probably be explained better - I'm open to comments.

Related

UILabel: Margin after using preferredMaxLayoutWidth with Autolayout

I have some weird margins in a UILabel when setting the preferredMaxLayoutWidth property of a UILabel inside a UITableViewCell. Do you know some behaviour like that?
I added the preferredMaxLayoutWidth like that:
- (void)layoutSubviews {
self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.frame.size.width;
[self.titleLabel sizeToFit];
self.shortDescriptionLabel.preferredMaxLayoutWidth = self.shortDescriptionLabel.frame.size.width;
[self.titleLabel sizeToFit];
[super layoutSubviews];
}
The Result is something like that:
As you see the title label has 14px margins and I don't know where they are from.
I already tried to call sizeToFit, but that doesn't helped.
The problem here is that the label's width is set to the width of the storyboard or xib which it is instantiated from (check the width inside of layoutSubviews and you'll see that it is very wrong). You will have to set preferredMaxLayoutWidth outside of this. What I have ended up doing is setting preferredMaxLayoutWidth to the width of the tableView minus any padding for the labels (I used a constraint group for this), which should be the same as the cell width.
So you'll need the following in method you do your cell configuration in (called from both tableView:cellForRowAtIndexPath: and tableView:heightForRowAtIndexPath:

Can't move my UIScrollView

I have a content size 1000x10000 and it's center is on the center of the viewcontroller.
I want to push a button for it to turn pages. In the next code I've tried almost any number in the origin.x but nothing changes the scroll view.
I don't understand the math of it but when I set origin.x=300 and origin.y=100 the view will move up a bit but that's it.
CGRect frame=scroller.frame;
frame.origin.x=ANY NUMBER HERE;
frame.origin.y=0;
[scroller scrollRectToVisible:frame animated:YES];
How can I set it to move the view from right to left
Try this,
[scroller setContentOffset:CGPointMake(300, 0) animated:YES];
setContentOffset:animated:
Sets the offset from the content view’s origin that corresponds to the receiver’s origin.
You should be creating the UIScrollView's frame to the size of the UIViewController like so:
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:self.view.frame];
Then set the scrollers CONTENT VIEW to the 1000x10000 size like so:
scroller.contentSize = CGSizeMake(1000, 10000);
The contentSize is what makes a UIScrollview able to scroll, as long as the contentsize is larger than the scrollers frame.
If you want to make it scrollable, then contentSize has to be greater than frame.size, otherwise there isn't space to scroll.
Then ensure that scrollEnabled is set to YES (by default it is, so unless that you've changed it it's already set to YES).

UIScrollView: handling of dynamic content?

I've a Storyboard with a UIScrollView which contains two UILabels, a UIImageView and a UITextView. The content of the UIImageView and UITextView is dynamic and so are their height.
Currently I'm doing this inside my viewDidLoad to adjust the size of the UITextView after the dynamic text is inserted:
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Is this the way to change its height?
My next problem is to set the content size for the UIScrollView, to activate the scrolling. Is there a smart way to get the height of all its content or do I have to calculate the height for each element and set the sum of this as the content size of the UIScrollView?
IF you had no space in between your objects, you could make a for loop in your scrollView.subviews and add up all the heights to set as the contentSize.
As you probably don't have everything tight together, you're probably better by getting the bottom most object and adding up it's frame.origin.y and it's frame.size.height (maybe you want to have some extra space in here, but that's up to you) and that will give you your contentSize.height to keep everything in there.

Setting the UITextView height to fit the content height in Objective c

I have the code as below for my UITextView set up.
CGRect itemDescFrame = CGRectMake(20, 160, 280, 200);
UITextView *itemDesc = [[UITextView alloc] initWithFrame:itemDescFrame];
...
itemDesc.editable = NO;
itemDesc.scrollEnabled = NO;
...
I have disabled the UITextView scroll bar, however some of the content on the bottom is being hidden. How can I set the UITextView height to fit to the content height?
Thanks
UPDATE:
NSLog(#"frame %#", itemDesc.frame.size.height);
NSLog(#"content %#", itemDesc.contentSize.height);
Result:
2011-10-11 11:00:18.010 abc[1606:207] frame 200.000000
2011-10-11 11:00:18.010 abc[1606:207] content 200.000000
As a subclass of UIScrollView, UITextView has a property called contentSize which, in the case of a UITextView, should be the size that the text occupies after it is is placed in the view. Try setting the height of the text view's frame to the height of contentSize.
Simply add this line of code to force the frame to update its size based on the height of the text it contains:
[self.myTextView sizeToFit];
Note that if you want to add additional padding you can set textContainerInset.
You'll want to call that when the text content changes and upon rotation. To make it easy to see the height adjust, set a background color on the UITextView.

How to make height of OHAttributedLabel scale with content height?

I use an OHAttributedLabel called demoLbl for displaying text with formatted areas. This label is laid out with Interface Builder and is connected to a property in my ViewController. After setting the attributedText to the label I want all the text to be displayed in the label.
If I don't resize the label then the text is cropped at the end of the label so the rest of the text is missing.
If I use [demoLbl sizeToFit]; then the height of the label is larger or smaller in height than the text (about 10 point, varying with the text's length) thus giving me blank areas at the bottom of my view (after scrolling) plus the width of the label is increased by about 2 points.
If I calculate the height of the original text (NSString) before putting it in a NSAttributedString and adding it to the label's attributedText property then the calculated height is way too small for setting it as the label's height.
Is there a hack or trick I can apply so that the label's height is adjusted according to the NSAttributedString's height?
PS: To be more specific I wanted to add OHAttributedLabel as a tag but it's not allowed to me yet.
I'm the author of OHattributedLabel.
I made some fixes recently about my computation of the size. Please check it out it will probably solve your issue.
I also added a method named sizeConstrainedToSize:fitRange: in NSAttributedString+Attributes.h that returns the CGSize of a given NSAttributedString (quite the same way UIKit's sizeWithFont:constrainedToSize: works, but for Attributed strings and CoreText and not plain stings an UIKit)
Actually OHAttributedLabel's sizeThatFits: calls this method itself now.
You can see if this category gives you a more reliable height.
https://gist.github.com/1071565
Usage
attrLabel.frame.size.height = [attrLabel.attributedString boundingHeightForWidth:attrLabel.frame.size.width];
I added this code to the implementation of the OHAttributedLabel class:
// Toni Soler - 02/09/2011
// Overridden of the UILabel::sizeToFit method
- (void)sizeToFit
{
// Do not call the standard method of the UILabel class, this resizes the frame incorrectly
//[super sizeToFit];
CGSize constraint = CGSizeMake(self.frame.size.width, 20000.0f);
CGRect frame = self.frame;
frame.size = [self sizeThatFits:constraint];
[self setFrame:frame];
}
// End Toni Soler - 02/09/2011
Thank you Olivier for sharing your code!