如何在UITextView上强制执行水平滚动?

时间:2010-05-13 13:09:18

标签: iphone objective-c cocoa-touch uikit

我有一个UITextView,我只想让它只能水平滚动。

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

2 个答案:

答案 0 :(得分: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];

我希望这就是你要找的东西。

答案 1 :(得分: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);

当然,您必须使用各自的规格替换文字和字体