Add "padding" to a UITextView

magtak picture magtak · Oct 26, 2011 · Viewed 50.3k times · Source

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?

Answer

Mário Carvalho picture Mário Carvalho · Nov 29, 2013

Using iOS7 I have done it just with

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

Hope this helps, Mário