Mihai Fonoage
Mihai Fonoage

Reputation: 478

UITextView scroll only horizontally

I have one line of text that is too long to be displayed in one single line in a UILabel, so I switched to a UITextView. I wanted to display that text still in one line, and be able to scroll horizontally, but instead, the UITextView will wrap the text into multiple lines, and allow me to scroll vertically. So, for the text "This is a very very long line", instead of displaying it like "This is a ", and then if scrolled horizontally, it would display the rest of it, it actually displays it like "This is a \n very very long \n line". How can I get the behavior I want?

Thanks, Mihai

Upvotes: 0

Views: 1300

Answers (2)

Jeremy Bader
Jeremy Bader

Reputation: 382

Using Merrimack'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: 0

Merrimack
Merrimack

Reputation: 1726

If you want that behavior you should place the label inside a scrollview. That's the only way you'll be able to scroll a label in any direction.

Upvotes: 3

Related Questions