Search code examples
iosobjective-cxcodeuitextviewword-wrap

UITextView disable wrapping


I'm making an application for iOS 7 and iOS 8 and I would like to disable word wrapping in my UITextView. I have some ASCII tables and I don't want them broken.

I didn't find the option in the StoryBoard and I tried to do it programatically. I have tried:

[textView setContentSize:[[textView attributedText] size]];

and

[textView.textContainer setSize:[[textView attributedText] size]];

Nothing seems to be working.


Solution

  • The UITextView was created for this exact functionality that you are trying to remove. It might be that the UITextView is not the best option based on your use case. There are many ways to accomplish this without the use of UITextView. Here are a few:

    1) If the text is not going to be editable, you could use a UILabel. Set the Number Of Lines = 0 and appropriate settings for Line Breaks and Autoshrink.

    2) If the text needs to be editable, you should use a UITextField inside of a UIScrollView.

    3) Make a custom UIView that calculates the space needed and draw the text yourself. (This option is the most robust, but usually is not necessary and can introduce and handful of issues. The afore mentioned controls will have this type of logic already worked out for you by Apple).

    Hope this helps.