JAHelia
JAHelia

Reputation: 7902

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.

Upvotes: 1

Views: 3284

Answers (3)

Jeremy Bader
Jeremy Bader

Reputation: 382

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

Upvotes: 1

JAHelia
JAHelia

Reputation: 7902

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.

Upvotes: 3

rishi
rishi

Reputation: 11839

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)];
}

Upvotes: 5

Related Questions