UITextView content inset

IosObjective CUiscrollviewUikit

Ios Problem Overview


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)

Ios Solutions


Solution 1 - Ios

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 - Ios

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:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionH BellamyView Question on Stackoverflow
Solution 1 - IosFlorianView Answer on Stackoverflow
Solution 2 - IosSteph SharpView Answer on Stackoverflow