Forcing a UITextView to wrap properly
Asked Answered
L

2

9

I have a UITextView with the following text:

textView.text = @"aaa a a a a a  a zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";

When I run it in the CGRect that I provided it goes like this:

aaa a a a a a  a 
zzzzzzzzzzzzzzzzzzzzzzz <-text view width ends here
zzzzzzz

But I want it to work like a UILabel with numberOfLines = 0, which gives me this:

aaa a a a a a  a zzzzzz <-text view width ends here
zzzzzzzzzzzzzzzzzzzzzzz <-text view width ends here
z

With iOS 6, UILineBreakMode was deprecated. How do I achieve this now?

Lauter answered 23/1, 2015 at 8:38 Comment(0)
V
17

UILineBreakMode is deprecated but not NSLineBreakMode. You should use NSLineBreakByCharWrapping meaning that the word'll be cut after a character.

textView.textContainer.lineBreakMode = .byCharWrapping
Vanzant answered 23/1, 2015 at 8:41 Comment(3)
hmmm but do UITextView have a line break mode?Lauter
self.textView.lineBreakMode = NSLineBreakByCharWrapping;Elora
@chris textview.textContainer.lineBreakMode I guess :)Vanzant
V
1

Here is how you can do it in Objective-C

textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
Vey answered 12/6, 2019 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.