Solution 1:

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)

Solution 2:

UPDATE: This solution is out of date as of iOS 7.

See this answer below. In iOS 7.0, the textContainerInset property on UITextView was introduced.

Objective-C:

textView.textContainerInset = UIEdgeInsetsMake(0, 50, 0, 0);

Swift:

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

Or as Zeev Vax suggested, in Swift 5.0:

textView.textContainerInset.left = 50

Pre-iOS 7 solution:

I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar...

I'm afraid this is not how contentInset works with a UITextView. See Apple's documentation for contentInset where it states:

The distance that the content view is inset from the enclosing scroll view... Use this property to add to the scrolling area around the content.

The contentInset is added around the content.


You can change the contentSize in viewDidLayoutSubviews using the code you have included above:

- (void)viewDidLayoutSubviews
{
    self.textView.contentInset = UIEdgeInsetsMake(0, 50, 0, 0);
    
    NSLog(@"Content Size Before %f",self.textView.contentSize.width); //Logs: 280
    CGSize size=self.textView.contentSize;
    size.width=size.width-50;
    [self.textView setContentSize:size];
    NSLog(@"Content Size After %f",self.textView.contentSize.width); //Logs: 230
}

However, this causes the text to be cut off on the right side:

UITextView cut off on 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 container UIView. In your case, simply create a UIView 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) inside UIView container:

UITextView inside container UIView

If your UITextView does have a background, see:

  • How to set UITextView's content inset like Notes App
  • Stuff you learn from reverse-engineering Notes.app (see "iPadding" section)

Hope that helps, and if anyone can find a better solution I'd love to hear about it!