54

as the title says i am trying to add padding-like behavior to a UITextView. The textview is generated when the view is pushed inside my navigation controller and the following code occurs:

 self.textView.layer.cornerRadius = 7;
 //pretty stuff is pretty

 NSString *description = appDelegate.productInDisplay.description;
 [self.textView setText:description];
 //pretty stuff has content now


 CGRect frame = textView.frame;
 frame.size.height = textView.contentSize.height;
 textView.frame = frame;
 //set the UITextView to the size of it's containing text.

 self.textView.editable = NO;

  self.theScrollView.contentSize = CGSizeMake(320, 250 + textView.frame.size.height);
  //set the parent scrollView's height to fit some other elements with fixed size (250)
  //and the pre-mentioned UITextView

so, it all works and it's ok, but i want to add some padding on all 4 sides of the UITextView and i've been unable to do this with 3 hours of googling for something that seems rather easy. Any suggestions?

magtak
  • 890
  • 1
  • 9
  • 20
  • 29
    it's just .. uitextview.textContainerInset = UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right ... **it's that simple** – Fattie Dec 12 '13 at 13:52

8 Answers8

94

Using Objective-C I have done it just with

[self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];

For Swift you can use:

textview.contentInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)

You can also create a Swift Extension (Suggested by chowdhury-md-rajib-sarwar) here:

extension UITextView {
func leftSpace() {
    self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
}

}

And then use

let textView = UITextView()
textView.leftSpace() 

Hope this helps, Mário

Mário Carvalho
  • 2,946
  • 3
  • 18
  • 25
  • 1
    This worked for me in iOS 7. In fact, it's the only one of the suggested answers that worked. – Trevor Gehman Jan 20 '14 at 22:31
  • [Mário Carvalho](http://stackoverflow.com/users/1557663/mario-carvalho) any possible alternative for "setTextContainerInset" in iOS 6 ? – GoGreen Mar 24 '14 at 05:41
  • 1
    @Mário Carvalho the problem is, I need support for both iOS 6 and 7, so, that answer will not be feasible. I found this [answer](http://stackoverflow.com/questions/18560412/uitextview-content-inset/18604414#18604414) to be more meaningful but it still didn't solve my problem. I finally ended up using a container view for my UITextView. Thanks for the help though! – GoGreen Mar 25 '14 at 04:05
  • Worked for me on iOS7 – John O'Connor May 15 '14 at 13:56
  • 3
    In Swift I used self.textView.textContainerInset = UIEdgeInsetsMake(50, 50, 50, 50); – FredL Oct 25 '14 at 07:54
  • This doesn't seem to work if your text alignment is right. – user1366265 Aug 11 '16 at 13:02
  • 1
    @KayanAlmeida yes! Check insets on storyboard options – Mário Carvalho Mar 27 '17 at 10:35
64

This answer is totally wrong. Correct answer is simply:

uitextview.textContainerInset =
       UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right

(Those values generally match how a UITextField on the same screen looks.)


Use this one:

self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5); 
Fattie
  • 30,632
  • 54
  • 336
  • 607
Sergey Metelin
  • 1,004
  • 6
  • 20
  • that puking textContainerInset doesn't work if you round textview's corners programmatically but that contetInset has no problem at all. Thanks ! – ACAkgul Jan 17 '19 at 10:55
26

EDIT 1/16/2015: This was written in 2012 and is no longer accurate. Use -textContainerInset as mentioned above.

Original post:

Using contentInset won't actually work. You have two reasonable choices: subclass UITextField and override the textRectForBounds: and editingRectForBounds: methods or create your text field transparently over a UIView and style the UIView.

Example of subclassing the UITextField:

- (CGRect)textRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

Example of wrapping it in a UIView:

UIView *wrapView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 30)];
[wrapView addSubview:textView];
wrapView.layer.borderColor = [UIColor darkGrayColor].CGColor;
wrapView.layer.borderWidth = 2.0;
wrapView.layer.cornerRadius = 5.0;
Jamon Holmgren
  • 20,854
  • 4
  • 52
  • 69
  • This should be the accepted answer – the current one doesn't actually work. – Nick Baicoianu Oct 18 '12 at 02:30
  • This is better than adding empty leftView. With leftView sometimes UITextField does not become first responder when touching placeholder text area (on iOS 5). – Haitao Li Feb 05 '13 at 00:06
  • 20
    I don't understand how this is accepted as the correct answer. The question is for UITextView, not for UITextField. UITextView doesn't have such methods, so at least the first part of the answer is not correct. The second part has a problem, because the scroll indicator is going to be shown indented, too. – José Manuel Sánchez Jul 29 '13 at 14:57
25

This is working in Swift 5:

textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
adamfootdev
  • 850
  • 5
  • 15
12

This worked for me using swift 2.0:

textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10)

Julian B.
  • 3,163
  • 2
  • 25
  • 35
6

For Swift 4.2:

textview.contentInset = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)
LAD
  • 139
  • 4
  • 12
4

For Swift 3 - the answer appears to be similar, but slightly different:

textview.contentEdgeInsets = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)

(Actually, I used the above for a UIButtonView, but as it is so closed to the answers that were posted for UITextView I'm thinking [hoping] that it applies for that too)

Adam Stoller
  • 933
  • 7
  • 14
  • 4
    Sorry for downvoting, but in truth it doesn't apply here. On UITextView it's called textView.textContainerInset – Alex Wally Sep 21 '17 at 02:08
  • 2
    **Swift 4** version of the same: `textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 50)` – Codetard May 11 '18 at 07:31
2

Simply create an extension of UITextView using Container Edge Inset as following:

extension UITextView {
    func leftSpace() {
        self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
    }
}

and use it like:

let textView = UITextView()
textView. leftSpace()