2010-05-13 58 views
1

我有一个UITextView,我只想让它只能水平滚动。如何在UITextView上强制水平滚动?

基本上,当UITextView文字换行时,我希望用户必须水平滚动才能查看其余内容。

+0

你想,或者在很长的行中列的文本? – 2010-05-13 13:10:34

+0

我希望文本只在一行上 – 2010-05-13 13:12:11

回答

4

您可以将文本视图的内容大小设置为视图的高度。只要确保内容的宽度超出了textView的框架宽度,否则它将不会滚动。

// 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]; 

我希望这就是你要找的。

4

解决方法是关闭UITextView本身的所有滚动选项,然后将其嵌入到另一个UIScrollView中。您可以通过在谷歌搜索这个术语“DualScrollTextView”来获得实际的代码。您无法强制更改contentSize来完成此操作 - 许多人都尝试过。

0

这为我工作:

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

当然,你会与你相应的规范,以取代文本和字体