I have encountered something a bit strange with contentInsets
I have a UITextView in my storyboard with a contentInset of 50 left, as I'm trying to add some padding to my uitextview
However, a scrollbar appears on the bottom of the uitextview, as shown below in this test:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar, so how can I remove the need for the horizontal scrollbar and make everything--the inset AND all the text in the uitextview--visible without the need for this scrollbar.
N.B: I'm not asking about preventing the scrolling horizontally or not displaying the scrollbar(thus cutting of the text)
Thanks a lot!
For atomk(UITextView is called ss)
NSLog(@"Content Size Before %f",self.ss.contentSize.width); Logs: 280
CGSize size=self.ss.contentSize; size.width=size.width-50;
[self.ss setContentSize:size];
NSLog(@"Content Size After %f",self.ss.contentSize.width); Logs: 230
There is no visible difference between the view with the code added than before it was added, so something's going wrong! (Thanks)
In iOS 7
UITextView
is based on TextKit and has a new propertytextContainerInset
. It behaves just as you would expect:UPDATE: This solution is out of date as of iOS 7.
See this answer below. In iOS 7.0, the
textContainerInset
property onUITextView
was introduced:Pre-iOS 7 solution:
I'm afraid this is not how
contentInset
works with aUITextView
. See Apple's documentation forcontentInset
where it states:The
contentInset
is added around the content.You can change the
contentSize
inviewDidLayoutSubviews
using the code you have included above:However, this causes the text to be cut off on the right side:
The best way I have been able to achieve the appearance of horizontal padding in a
UITextView
is to position it inside a containerUIView
. In your case, simply create aUIView
the same size as your current text view, and add a text view that is 50px narrower inside the container view.This workaround can cause problems if you have a background for your text view, but from your screenshot above it doesn't look like that's an issue for you.
UITextView
(frame in red) insideUIView
container:If your
UITextView
does have a background, see:Hope that helps, and if anyone can find a better solution I'd love to hear about it!