Expand a UITextView when clicked? - objective-c

How would I go about animating open a textviewto expand when clicked? So if I have a UITextView that is of height 30 but when it gains focus have it expand to 100. Then on losing focus have it go back to 30 even if the text is of height 100.
TIA

Look into the UITextViewDelegate methods -textViewDidBeginEditing: and -textViewDidEndEditing:

Interesting you ask this, because I literally just figured out how to do the same thing. Call this method whenever you want to resize your textview:
-(IBAction)resizeFrame {
textView.frame = CGRectMake(x, y, width, height);
[self.view bringSubviewToFront:questions];
}
Line 1 sets the frame of the textView as a rectangle starting at point x,y with whatever width and height you specify.
Line 2 brings this view to the front so that you can actually see it!
If you want to size it back down, call another method with a different name that sets the CGRect back to the textView's original size.

Related

Change ViewContainer size height

I have a ViewContainer in my ViewControler, and I wanted to ask you guys if it's possible to change the size of the ViewContainer (change the height over a gesture) when the app is running.
Thanks!
You can change size of UIView of viewcontroller using below code
Call this method whenever you need to change frame size of view
Sample code below
Set view height to 100
[self.view setFrame:CGRectMake(0,0,320,100)];
To change height to 80
[self.view setFrame:CGRectMake(0,0,320,80)];
This code is good, but static.
I need it to change the height of the ViewContainer with a gesture with the finger.

Interface Builder Autolayout and Resizing

So I have two questions about Interface Builder for Xcode:
I'm trying to build an interface with a label on the left and a text field on the right, which have constant spacing between them, but I want the text field to expand horizontally when I resize the window horizontally to keep the spacing. I've added a restraint that keeps the spacing between them equal, but when I move the window, it resizes the label box rather than the text field. I tried pinning the width of the label, but then it stops me from resizing the window.
Is there any way to resize multiple items at the same time? Like if I have 8 labels vertically and I want to size them all to each be an 8th of the window space, how can I do that without just eyeballing it? It would be easy if you could highlight all of them and drag one corner to resize them all, but it wont let me do that.
For your first problem you should uncheck AutoLayout from Utilities Panel -> File Inspector and see what happens. Concerning the second you can create how many labels you want with your desired size directly from code as follows :
Example for 10 labels:
for (int i=0; i<30; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:anyFrame];
[yourView addSubView:label];
[label release]; //if not using ARC
}

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.

Why do updates on "contentOffset" of a UIScrollView make no effect?

I have a UIScrollView which is scrollable both vertically and horizontally. This view is filled with lots of buttons, each of them with its own width (but all with the same height).
When one of these buttons gets tapped, a slider-like interface is brought to life. If this interface goes over the selected button, the whole scroll view must be scrolled so that the button becomes visible once again.
My app behaves as expected when the Y coordinate of the scroll view's content offset is set to a limit (this limit can be 0 or the view's height). But if the content offset is located in an intermediate vertical position, the scrolling just doesn't seem to happen.
At first, I tried the following approach:
CGPoint newOffset = CGPointMake(self.scrollView.contentOffset.x + horizontalVar,
self.scrollView.contentOffset.y);
[self.scrollView setContentOffset: newOffset animated: YES];
Which didn't work, as I mentioned.
Then, I tried to manually animate the view, using its property setter:
[UIView animateWithDuration: 0.3 animations: ^{
CGPoint newOffset = CGPointMake(self.scrollView.contentOffset.x + horizontalVar, self.scrollView.contentOffset.y);
self.scrollView.contentOffset = newOffset;
}];
That approach produced the following result: if the scroll view is in an intermediate vertical position when one of its buttons gets tapped, the content offset update causes a visual change, but the view almost immediately returns to its original state.
I have no other clues on the subject. Could you please help me?
Maybe it's because you didn't set the ContentSize of your scrollView.

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!