2

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.

Octan
  • 348
  • 4
  • 19

5 Answers5

5

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.

reshat2
  • 213
  • 1
  • 7
  • Thank you very much, I'll test it on Monday and let you know. I need a multiline text and I don't know if the `UITextField` will fulfill my requirements. – Octan Nov 01 '14 at 14:26
  • 3
    I was able to do it using a `UILabel`. Unfortunately it does not work for label with a size greater than 10000 pixels. – Octan Nov 03 '14 at 09:00
  • That is a shame, though I imagine a UILabel does need some restrictions. – reshat2 Nov 03 '14 at 18:06
  • 2
    *The UITextView was created for this exact functionality that you are trying to remove.* It was created specifically to wrap text to the next line? – Steve Jan 12 '19 at 07:38
  • 1
    It doesn't seem like UITextField supports multiple lines? Or at least I wasn't able to find a way to configure it to accept more than one line, can you elaborate on the UITextField option? – jrh Jul 07 '19 at 01:49
0

I'm not sure exactly what your interface looks like, but here are a few options:

You could either try this answer to detect a word wrap and go from there. Word wrap detecting in UITextView

It might work for you. Or try using a bezier path to draw the line how you want it. Or NSTextContainer like here Adding exclusion paths to multiple text views

Community
  • 1
  • 1
MendyK
  • 1,643
  • 1
  • 17
  • 30
0
CGSize txtSize = [editor sizeThatFits:CGSizeMake(0, 0)];
editor.frame = CGRectMake(0, 0, txtSize.width, self.frame.size.height);
self.contentSize = CGSizeMake(txtSize.width, self.frame.size.height);

Here "self" is a UIScrollView and will scroll the UITextView horizontally. "editor" is a UITextView and will scroll itself vertically.

Calm Down
  • 1
  • 1
0

You can also "disable" word-wrap in UITextView by putting it inside UIScrollView and setting

textView.isScrollEnabled = false
Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67
0

The question specifies the need for horizontal scrolling, but if you want to simply disable word wrapping and cut off the text that doesn't fit, you can do this:

let attributedString = NSMutableAttributedString(string: your_string)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byClipping // .byTruncatingTail also works
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
your_text_view.attributedString = attributedString
arlomedia
  • 8,534
  • 5
  • 60
  • 108