UITextView content inset

H Bellamy picture H Bellamy · Sep 1, 2013 · Viewed 60.1k times · Source

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:enter image description here

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)

Answer

Florian picture Florian · Nov 18, 2013

In iOS 7 UITextView is based on TextKit and has a new property textContainerInset. It behaves just as you would expect:

UITextView *textView = ...;
// Left inset of 50 points
textView.textContainerInset = UIEdgeInsetsMake(0.0, 50.0, 0.0, 0.0);

Swift 4.2

textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)