UITextView content size calculation after setting text - ios

I've added a UITextView with Autolayout constraints in Storyboard. I am filling it up with text coming via API response. I want to use a custom vertical progress bar as user scrolls through UITextView. For this I need to know the current position and total height of content in UITextView. When I write this code in
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(#"Hello : %f %f",_readerTextView.contentSize.height, _readerTextView.contentOffset.y);
}
I get the current position but I don't get the total content height during initial stages of scrolling. I only get the total content height as I reach bottom of UITextView. How do I get the total content height as and when I set the text in my UITextView?

Related

Let uiscrollview dynamically adjust to uitextview height

My structure consist of
- uiview
- scrollview
- contentview
- uitextview
- uibutton
What I'm trying to achieve is to adjust the height of scrollview according to textview height or content. Textview gets its text on run accordingly. So when i have a long text in my textview, scrollview adjusts itself so all textview's content is visible by scrolling.

Scrollview content size set to screen width max

In my view I have a scrollview.
In my scrollview I have a UILabel.
I use constraints to set my scrollview in my superview and also for my UILabel.
If I set a long text inside my UILabel the scrollview's content extends to the width of my UILabel.
Instead of that, I would like the content view set to maximum width of my screen and the UILabel goes to multilines.
How can I do this ?
Try setting your label's number of line to 0 and preferredMaxLayoutWidth the width you want, then call setNeedsLayout with your main view.

Getting the visible label text from UIScrollView

I have added 10 labels to to display 0 to 9 in UIScrollView, User can see only one label in UIScrollView visible part. User needs to scroll to see other labels. How to determine which label is currently visible in UIScrollView after scroll view decelerating.
Thanks in advance
Use the scroll view's contentOffset and calculate how many "pages" down they have scrolled by dividing the y offset by the content size height.
When scrolling is complete compare contentOffset value with labels positions or view to see which label is currently shown:
Use this method for getting scrolled position:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(#"%f", scrollView.contentOffset.y);
//your logic to check shown label...
int currentVisiblePage = (scrollView.contentOffset.y / self.view.frame.size.height) + 1;
}
if you just want a single scrollable label, would be to use UITextView instead (reference). Disable editing, and you get a scrollable label.
(Taken almost verbatim from: how to add a scroll function to a UILabel)
For more detail:
UILabel inside of UIScrollView – programmatically
Sample code
AutoScrollLabel

How to get a title to scroll above an iOS text view

I want to have a text title scroll above a text view, but can't find information on how to do it. I've been looking for days, but can't even figure out how to look for the info. I want the function to work like the Apple Notes app where the date text is positioned above the text entry location and scrolls off the screen with the text but is not editable.
I've tried placing labels above UITextView, but the label does not scroll with the textView. I have a sample Xcode project I'm work with, but not sure if it can be uploaded for others to see what I'm doing. I almost had success with the project, but the labels only scroll with the text in the landscape view, not the portrait view for some reason.
I've read Apple developer docs on TextViews and several other sources without finding any discussion on how to do this or examples to follow.
Can anyone point me in the right direction?
I'm sure there must be a better way to do this, but this is the only approach I've found to work after more than a week of trying to find a solution. If I can figure out how to upload the entire Xcode example project I will do that.
This is the explanation of the approach I found to work under iOS 6.1 and Xcode 4.5. I have only tired this on the iPhone 6.1 simulator.
After creating a full screen text view and placing a label at the top of the text view,
To get the label to scroll off screen with the editable text view you need to:
turn off autolayout in interface builder (uncheck Use Autolayout). (there appears to be a problem with autoLayout and scrollViews in iOS 6.x)
turn off the text view scrolling in interface builder (uncheck Scrolling Enabled)
set the text view content insets height (Top) to the label height
embed the text view and label in a scroll view.
set properties for the scroll view and text view to be able to access their properties
in viewDidAppear:animated:
a. set the scroll view content size height to 20 plus the greater of the screen view height or the text view content height plus the label height
b. set the text view delegate to self
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
float screenHeight = self.view.bounds.size.height;
// set the scroll view content height to 20 plus the greater of the view height or the text view content size height plus the label height
screenHeight = MAX(screenHeight, self.textView.contentSize.height + 20);
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 20 + screenHeight);
// set the text view delegate
self.textView.delegate = self;
}
set the text view height dynamically in textView delegate method textViewDidChange:
a. if the text view content height is greater than the view bounds height less the label height:
1. set the text view frame to the view bounds width and the text view content height plus the lable height
2. set the scroll view content height to the view bounds width and the text view bounds height plus the label height
// dynamically set the text view content size height
-(void) textViewDidChange:(UITextView *)textView {
if (self.textView.contentSize.height > self.view.bounds.size.height - 20) {
self.textView.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.textView.contentSize.height + 20);
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.textView.bounds.size.height + 20);
}
}
set the struts and springs in interface builder
1. set the scroll view and text view struts for left, right and bottom
2. set the scroll view and text view springs for width and height
3. set the label struts for left, right and top
4. set the label spring for width
The same settings need to be made every time the view changes orientations.
There might be a way to do this by setting constraints in code under autolayout, but I barely understand constraints, much less setting them successfully in code.
Hope this helps someone else.
What you are looking for is called UIScrollView. Take a look at the Apple documentation here.
A good tutorial on UIScrollView can be found on the Ray Wenderlich site.

UIScrollView how to set ContentInset and ScrollerInset

im not able to configure my UIScrollView.
The image above shows my view. The orange box is my UIScrollView and the blue boxes (plus the orange box) is the view that i want to scroll.
The View is 520px with and the UIScollView (as usual) 320px.
How have I to set the ContentInset and ScollerInset?
Since the heights of the scroll view and its content match, and because it does not appear from your picture that you would like to add an empty space to the content, you should keep the contentInset set to zero (as it is by default). If you would like the content's middle to be visible through the scroll view, set content offset to (520-320)/2 = 100, like this:
[scrollView setContentOffset:CGPointMake(100, 0) animated:YES];
// If you do not want to see the view scroll, change this ^^^ to NO
I'm not sure exactly what your question is, but I'm guessing your scrollview is not scrolling as expected.
You need to set the contentSize property to tell the scrollview that there is more content than just the viewable area (e.g. myScrollView.contentSize = CGSizeMake(520.0, 100.0);)
If you want to start with the content centred as per your diagram, you can to set the contentOffset property. (e.g. myScrollView.contentOffset = CGPointMake((myScrollView.contentSize.width - myScrollView.frame.size.width) / 2, 0.0);)

Resources