uiscrollview for scrolling a very, very tall page - uitextview

From top to bottom I have UIView, UIScrollView, a UIImage, a UILabel, a UITextView and a UIButton.
My reason behind the top-most UIScrollView was so the whole vertical content would scroll.
What I really need a substitute for is the UITextView (5th down) because the UITextView is a subclass of UIScrollView. And this substitute must accomodate the very tall column of formatted text.
What I don't want is a scrollable object in the middle of the page; I want the whole page to be scrollable.
One more thing ... please note there's a button immediately below this tall column of text.
John Love

THANK YOU! Ashack and Pentagp. I guess I finally had to surrender and dump the idea that IB could be used for all GUI ...
The entire UIView contains a UILabel, a UIImage, a UITextView and
a UIButton at the very bottom.
The goal here was to make this entire content scrollable.
Thanks to you guys at stackoverflow.com:
using IB, un-check "Scrolling Enabled" for the UITextView
because it's a concrete type of UIScrollView
using IB, drag a UIScrollView to the UIView and match sizes and
insure that the UIScrollView encloses all sub-views
make the UITextView height = its contentSize.height
move the UIButton to below this UITextView
and then, thanks to iphonedevsdk.com:
adjust the contentSize.height of the top-most UIScrollview to
include the height of the re-positioned UIButton
Problem solved!!!

Your question is not very specific, but I think you're asking how to make a text view that does not scroll, and expands to fit the text. You can do this with UILabel, but it's a multi-step process. The same will work for UITextView if you disable scrolling and set the contentSize to match the frame dimensions.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100,100,100,100)];
label.font = [UIFont systemFontOfSize:24];
NSString *yourTextString = #"your text";
CGSize maximumLabelSize = CGSizeMake(100, 500);
CGSize expectedLabelSize = [yourTextString sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
CGRect labelFrame = label.frame;
labelFrame.size.width = expectedLabelSize.width;
labelFrame.size.height = expectedLabelSize.height;
label.frame = labelFrame;
Then from there, use the label's frame to set the position of the button and all elements below it.

If I understand your problem, you don't want nested scroll.
So set yourTextView.scrollEnabled = NO; //Do it programmatically our in the nib
Also in order to display all the text set yourTextView frame height to its content height:
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x,
yourTextView.frame.origin.y,
yourTextView.frame.size.width,
yourTextView.contentSize.height + (yourTextView.contentSize.width > yourTextView.frame.size.width ? (yourTextView.contentSize.width * yourTextView.contentSize.height / (yourTextView.contentSize.width - yourTextView.frame.size.width)) - yourTextView.contentSize.height :0)));
/*
If textView is dynamically filled, you have to check if yourTextView.contentSize.width is bigger than yourTextView.frame.size.width then add proportionally what is remaining to the height.
*/
At the end change your button position if needed:
yourbutton.frame = CGRectMake(yourbutton.frame.origin.x,
yourTextView.frame.origin.y + yourTextView.frame.size.height + 20, //If you want your button to be 20 point after your textView
yourbutton.frame.size.width,
yourbutton.frame.size.height);
Hope that will help you...

Related

Xmarin: UILabel in UITableViewCell update width

My UILabel in UITableViewCell is too long so am trying to update it and then move another component closer to the label so they are next to each other.
I basically want to calculate the width of the label based upon the text length, make the label shorter then move a UIImageView next to it closer so you always have the UILabel + UIImageView right next to each other without a big gap in-between even if the length of the text is different.
When i try to change the Width of the UILabel via code it doesn't take effect.
For example:
myLabel.Frame = new RectangleF ((float)myLabel.Frame.X, (float) myLabel.Frame.Y, (float) 2, (float) myLabel.Frame.Height);
If your using Autolayout then this code will not take effect. You should either disable Autolayout or add constrains to your UILabel with Autolayout
How to add Autolayout constrains with Xamarin

Animating changes in pages background color of a uiscrollview while user is swiping, synchronizing change in background color with offset change

any hint on achieving this, ie, animating changes in pages background color of a uiscrollview while user is swiping, synchronizing change in background color with offset change, back and forth ?
Regards,
j.
To make changes in real time while scrolling you should use UIScrollView delegate method:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
If you create 2 view controller and add their view as subviews of a scroll view, set the delegate property of your scroll view and add this code inside the delegate:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offset = scrollView.contentOffset.x;
CGFloat factor = offset / scrollView.bounds.size.width;
_leftVC.view.backgroundColor = [UIColor colorWithHue:factor saturation:1.0 brightness:1.0 alpha:1.0];
_rightVC.view.backgroundColor = [UIColor colorWithHue:0.3 saturation:1.0 brightness:factor alpha:1.0];
}
It just change the background color, but you can do everything you need synchronized with the scroll view offset.

UIScrollView messes up its subviews

I have got a UIScrollView with some subviews. The subviews can be expanded and folded. When the UIScrollView loads, the subviews are folded and there is no need for the UIScrollView to enable scrolling. When I expand a subview, the subviews under the expanded subview moves down. Now it can happen that the content size of the UIScrollView is not large enough when some subviews are expanded. So I try to update the contentSize like this:
`float scrollViewSizeHeight = ingredientsView.frame.size.height + recipeView.frame.size.height
+ tasteView.frame.size.height + nutritionView.frame.size.height + 300;
if (scrollViewSizeHeight > scrollView.frame.size.height)
{
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollViewSizeHeight)];
[scrollView setNeedsDisplay];
}`
New code tag is weird -.-
But when this happens, the subviews collapse and the positions get messed up. Anyone any idea why?
Just deactivate autolayout in IB

How to adjust size of UITextView automatically ?

I want to make speech bubbles but there is a problem adjusting size of the bubbles in UITextView.
is there a way to increase the size of the Textview automatically depending on the length of the
text by using UITextView?
Okay I found a better way of doing what I was suggesting. Rather then deal with the UIView and resizing depending on the text inside it. I just made the UITextView have a rounded edge which now looks like a panel. Perfect. Hope this helps someone!
If your interested in the code
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
self.clipsToBounds = YES;
self.layer.cornerRadius = 10.0f;
}
I think I understand what your getting at. I had this problem the other day. This bit of code will adjust the UITextView contents. Create a new class and call this into it via a new class. If you want to adjust the View well I'm still working on that :-P
Hope this helps
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
}

Monotouch - make UIView scrollable

I have a view with 4 text boxes and and a logo at the top - when the user is entering information the text pad covers up some of these controls, how can I make the view scroll so that this isn't an issue.
I have tried adding the view to a UIScrollView but that doesn't seem to do anything?
I've included a snippit below of how I've handled your situation. If I'm understanding you correctly, you do not wish to have a scrollable view, rather you want to the view to move in conjunction with switching to and from fields to alleviate and visual hindrances caused by the keyboard.
Goodluck!
private void ScrollTheView(bool movedUp, float scrollamount, UIView ViewToMove)
{
//To invoke a views built-in animation behaviour,
//you create an animation block and
//set the duration of the move...
//Set the display scroll animation and duration...
UIView.BeginAnimations(string.Empty, System.IntPtr.Zero);
UIView.SetAnimationDuration(0.15);
//Get Display size...
RectangleF frame = ViewToMove.Frame;
if (movedUp) {
//If the view should be moved up,
//subtract the keyboard height from the display...
frame.Y -= scrollamount;
}
else {
//If the view shouldn't be moved up, restore it
//by adding the keyboard height back to the original...
frame.Y += scrollamount;
}
//Assign the new frame to the view...
ViewToMove.Frame = frame;
//Tell the view that your all done with setting
//the animation parameters, and it should
//start the animation...
UIView.CommitAnimations();
}
You need to set more to the UIScrollView than just put subviews in it. Set up the ContentSize property properly for the complete size of the subviews so the scrollview knows about the larger content in it, than you can control the scrolling position, zoom factor and so on.
There are plenty of samples on iOS SDK, just check the UIScrollView documentation, transformation to Monotouch from ObjectiveC is straightforward or check blog post at http://blog.touch4apps.com/home/iphone-monotouch-development/monotouch-infinite-loop-image-scroll-view where I have a sample with images autoscrolled in UIScrollView.
something like this.
textBox.EditingDidBegin += delegate {
var offset = scrollView.contentOffset;
offset.Y -= 200;
scrollView.contentOffset = offset;
}

Resources