How to enforce horizontal scrolling on UITextView? - iphone

I have a UITextView and I would like to only allow it to be scrollable horizontally only.
Basically when the UITextView word-wraps I want the user to have to scroll horizontally to be able to view the rest of the contents.

You can set the content size of the text view to the height of the view. Just make sure the width of the content extends past the width of the textView's frame or else it won't scroll.
// UITextView* myTextView: gets declared somewhere...
// The text view (subclass of a UIScrollView) won't go past its content size
[myTextView setContentSize:CGSizeMake(width, myTextView.frame.size.height)];
// Just in case, if you don't want your user bouncing the view up/down
[myTextView setAlwaysBounceVertical:NO];
I hope that's what you were looking for.

The solution is to turn off all the scroll options on the UITextView itself, then embed it in another UIScrollView. You can get actual code by searching on this term "DualScrollTextView" in google. You cannot force changes to contentSize to accomplish this - many have tried.

This worked for me:
self.dishNameLabel.text = #"Here is some very longgggg text to test with";
float width = ceil([self.dishNameLabel.text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"WorkSans-SemiBold" size:15.0]}].width);
self.dishNameLabel.frame = CGRectMake(self.dishNameLabel.frame.origin.x, self.dishNameLabel.frame.origin.y, width, self.dishNameLabel.frame.size.height);
self.dishNameScrollView.contentSize = CGSizeMake( width, self.dishNameScrollView.frame.size.height);
Of course, you will have to replace your text and font with your respective specs

Related

UIScrollView setting page size dynamically

I've done quite a bit of looking into this but have been unsuccessful in finding a solution.
I have a UIScrollView called scrollView inside of UIView. I'm using this scrollView in pagingEnabled mode. Inside of scrollView, I have 3 different views created programmatically. Inside of these 3 different views, I have a bunch of stuff(UILabel,UITextView etc.). All of the views contents are dynamic and determined at runtime.So, i really don't know scrollView.contentSize. If i give the content size of this scrollView,Sometimes ,I have a screen with white blank at the botton of the screen when user scrolls down. My question is : Can i set the content size dynamically for each single page of this scrollView? For example,for page 1 :
self.scrollViewNews.contentSize = CGSizeMake(constant1,constant2);
And set something else for page 2 as well .
I think you're confused about contentSize vs the paging size. The paging size is always the size of the scrollView bounds, and isn't a property you can otherwise set. That is, when you swipe left/right it will "page" by the width of the scrollview.
contentSize is the size of the virtual bounding box of all the subviews within the scrollview. This only serves to limit how far the scrollview will scroll, and for paging, how many times it will page, i.e. contentSize.width / bounds.size.width.
Assuming the scrollView isn't zoomed in/out (zoomScale = 1.0) then you need to position and size your subviews on the virtual 'page boundaries'. They can take up the full page boundary (be sized to match scrollview.bounds) or be inset. If you have some content that is larger/smaller then you'll have to decide if you want to change the scale of that content or size it up/down within the page bounds.
Yes you can dynamically set the content size of the scrollView. Also You can use this method:
self.scrollViewNews.contentSize = CGSizeMake(constant1,constant2);
Nothing wrong this method. You are seeing the blank space at bottom because you ares setting the height of the scrollView's contentSize to a larger value than it's content. That's the issue. Adjust the height according to the contents, blank space will go.
please, try to get the actual content size from the current content.
the content must be some inherited class from the UIView and it has a frame.size.width and frame.size.height property.
you can use those to set the contentSize of your UIScrollview for the current content at that time when you add the content to the UIScrollView.
Yeah. You can set the scrollView size dynamically through
scroller.contentSize = CGSizeMake(1650, 2000);
You can use the following property to set the dynamic frame for each pages
[urScrollView scrollRectToVisible:frame animated:YES];
if you are using pagingEnabled = YES for your scrollview
The below UIScrollViewDelegate delegate will adjust the page and content
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int page = urScrollView.contentOffset.x/urScrollView.frame.size.width;
pageControl.currentPage = page;
}
//pager action
- (IBAction)changePage:(id)sender{
int page = pageControl.currentPage;
CGRect frame = urScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[urScrollView scrollRectToVisible:frame animated:YES];
}
please look at this sample app
Try out this.
- (void)viewDidLoad
{
float sizeOfContent = 0;
UIView *lLast = [scrollView.subviews lastObject];
NSInteger wd = lLast.frame.origin.y;
NSInteger ht = lLast.frame.size.height;
sizeOfContent = wd+ht;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, sizeOfContent);
}

how to force horizontal scrolling only on UITextView

I have a UITextView with contentSize property set to a very large value 1000, the width of the UITextView is 200, scrolling is enabled, horizontal bouncing is enabled and vertical bouncing is disabled but this UITextView still scrolls vertically on large text.
how can I force horizontal scrolling on it ?
p.s. the solution here does't work.
UITextView is a subclass of - UIScrollView. So you can use ScrollViewDelegate method for disabling this.
Use -
- (void)scrollViewDidScroll:(id)aScrollView
{
[aScrollView setContentOffset:CGPointMake([scrollView contentOffset].x, 0.0)];
}
I have tried many different ways to have a horizontal scrolling functionality to work as expected, but non of these methods worked for me, so I created a UIScrollView and added a UILabel as a subview in the scroll view, and setting the frame's width of the label dynamically according to its text and the text font, after that I set the contentSize of the scroll view to be equal to the label's new frame width & height. Using this method you will have a horizontal scrolling text.
Using JAHelia's advice, this worked for me:
self.dishNameLabel.text = #"Here is some very longgggg text to test with";
float width = ceil([self.dishNameLabel.text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"WorkSans-SemiBold" size:15.0]}].width);
self.dishNameLabel.frame = CGRectMake(self.dishNameLabel.frame.origin.x, self.dishNameLabel.frame.origin.y, width, self.dishNameLabel.frame.size.height);
self.dishNameScrollView.contentSize = CGSizeMake( width, self.dishNameScrollView.frame.size.height);
Of course, you will have to replace your text and font with your respective specs

How to make this app screen scroll?

I've a screen in an app I'm coding structured like this:
View
ScrollView
View
label 1
label 2
label 3
View
UIImageView
WebView
When loaded it adds some html string into the Web View and as the whole content (labels,image,html content) is longer than the screen height, I would like to allow the screen to scroll down/up when user is reading content. This is why I added the SrollView but nothing scroll!
I also did is scroll_view.contentSize = [self.view bounds].size; but it didn't work
Any idea on how to do this ?
Thx in advance for helping,
Stephane
By default UIScrollView is set a contentSize equal to its frame size. If you want it to be scrollable, you have to explicitly set the contentSize.
scrollView.contentSize = CGSizeMake(_width, _height);
Mostly you won't be knowing the actual contents size while you create the scroll view. You can assign the contentSize after adding the UIWebview(as web view is the last view in your scroll view). You can do it like this,
// Add other views to scrollView
// Create and configure webView
[scrollView addSubview:webView];
float _width = scrollView.contentSize.width; // No change in width
float _height = CGRectGetMaxY(webView); // Returns (webView.frame.origin.y + webView.frame.size.height)
scrollView.contentSize = CGSizeMake(_width, _height);
Below code u observe Here "Height" declare Dynamically as your Requirement
EXample :
if([Myarray length]>25)
{
myString = [myString stringByAppendingFormat:#"<tr><th align=\"left\">%#</tr>",str_owes ];
height += 50;
}
webview.frame=CGRectMake(25, 153, 420, height);
[webview loadHTMLString:myString baseURL:nil];
scrollview.contentSize=CGSizeMake(0, webview.frame.origin.y+webview.frame.size.height+50);
You need to set the UIScrollView's contentSize to be the size of the two views + the UIWebView combined.
You will also need to make sure that the UIWebView if of the right size and then turn scrolling off, as you may get scrolling issues otherwise (one scroll view inside another).
More info: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html

UIScrollView problem, scrolls just on one side

I'm fairly new to programming, and I have looked for an answer for a very long time. There are some posts about it, but nothing has solved my problem. I have a UIScrollView view that I get from the nib, everything is ok with this, the content length is good and scrolling works, but it just scrolls on the left side, if I try to scroll on the right side it doesn't scroll..
Here is the code,
- (void)viewDidLoad {
[super viewDidLoad];
NSString *descriptionString = _currentBook.description;
CGSize stringSize = [descriptionString sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(387, 9999) lineBreakMode:UILineBreakModeWordWrap];
_authorLabel.text = _currentBook.author;
_titleLabel.text = _currentBook.title;
_descriptionLabel.text = [NSString stringWithFormat:#"Description: %#",_currentBook.description];
[(UIScrollView *)self.view setContentSize:CGSizeMake(387, stringSize.height +50)];
}
Thanks in advance!
Its hard to understand the problem since we cant see your nib file, but its better practice to put the scrollView on top the view of the view controller, and connect it to an IBOutlet in your view controller.
In order to find the problem I would get rid of the textfields for testing purposes (I think the constrained 9999 might be a problem but I am not sure) and then print and post the frame of the scrollView and the Content size in runtime.I am betting that you will see some issue with the frame of the uiscrollview.
Thanks,
Ok, after copy-pasting and running some tests, I found out the problem.
The problem is in the wording of the question, your problem is not that the "scroll doesn't work on the right side" (As in: you move your finger up and down on the right side of the screen without triggering a scroll), the problem is that the contents, the label itself is going out of bounds, outside of the visible area of the scrollView, and the right-handed side is not visible.
First of all, you should note that the iphone resolution is 320x480 (640x960 for Retina), so you actually have to work with a smaller width (using 387 width will make it go out of bounds).
Second, take in account the x position of the label itself is also affecting the amount of visible text. With this in mind, a more generic code would be:
- (void)viewDidLoad
{
[super viewDidLoad];
// This number represents the total width of the label that will fit centered in
// the scrollView area.
CGFloat visibleWidth = self.view.frame.width - descriptionLabel.frame.origin.x*2;
// Use the number above to get a more accurate size for the string.
NSString *descriptionString = _currentBook.description;
CGSize stringSize = [descriptionString sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(visibleWidth, 9999) lineBreakMode:UILineBreakModeWordWrap];
// Fill data (why so many underscores?)
_authorLabel.text = _currentBook.author;
_titleLabel.text = _currentBook.title;
_descriptionLabel.text = [NSString stringWithFormat:#"Description: %#", _currentBook.description];
// Also, why didn't you resize the description label? I'm assuming that you forgot this.
// (Make sure that the descriptionLabel number of lines is 0)
CGRect frame = descriptionLabel.frame;
descriptionLabel.frame.size = stringSize;
// Now set the content size, since you're using the scrollView as the main view of the viewController,
// I'll asume that it's using the whole screen, so I'm gonna use the whole width
// of the screen (that's what UIScreen is for).
[(UIScrollView *)self.view setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, stringSize.height +50)];
}
Finally i've found my problem, I've added a uiview programmatically as a subview to some view and then to this View i've added my scroll view as a subview.. And then it would only scroll in the area of my UIView. It was nonsense to do it like this, still a lack of knowledge..
Thank you all for trying to help!

How can i fix my scrollView dynamically when i have more than one textViews in it?

I have a scrollView which consists of 3 textViews, buttons and labels in a detailView. i am using 3 text view because i need different fonts for my view title, date and description. problem is sometimes description is long and sometimes its small and same thing with headings. then view doesn't loog good at all because of alot of empty spaces between title and description. Is it possible to set the size of textView and scroll view dynamically or is there any better approach to solve this problem. thanx in advance
You need to adjust the frames of the text views to match their respective contentSizes (see the top answer here: How do I size a UITextView to its content? on how to do that, uses the contentSize property of the textview), then you adjust the contentSize of the scrollView based on the frames of all the controls.
Assuming your textviews are placed consecutively vertically (just adjust the code if there is spacing, etc.):
// (first adjust each text view's frame per the linked answer)
...
// then adjust the frames of the content
CGRect topTextViewFrame = topTextView.frame;
CGRect middleTextViewFrame = middleTextView.frame;
middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
middleTextView.frame = middleTextViewFrame;
CGRect bottomTextViewFrame = bottomTextView.frame;
bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
// then adjust your other controls based on these frames, for example:
CGRect myButtonFrame = myButton.frame;
myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
// finally adjust the contentSize of the scrollview assuming the button is the bottom element
CGSize csize = myScrollView.contentSize;
csize.height = myButtonFrame.origin.y + myButtonFrame.size.height;
myScrollView.contentSize = csize;
You could set the size of the frame to be dependent on the character length by setting the frame at the onset to be:
CGRectMake (0.0, 0.0, [yourString count]*10, 30.0);
This is what I did when I had a UIPopover come up with a variable name.