开发者

How to enforce horizontal scrolling on UITextView?

开发者 https://www.devze.com 2022-12-30 17:46 出处:网络
I have a UITextView and I would like to only allow it to be scrol开发者_高级运维lable horizontally only.

I have a UITextView and I would like to only allow it to be scrol开发者_高级运维lable 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号