Add "padding" to a UITextView

IosUitextviewPadding

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

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); 

Solution 3 - Ios

This is working in Swift 5:

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

Solution 4 - Ios

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;

Solution 5 - Ios

This worked for me using swift 2.0:

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

Solution 6 - Ios

For Swift 4.2:

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

Solution 7 - Ios

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)

Solution 8 - Ios

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() 

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
QuestionmagtakView Question on Stackoverflow
Solution 1 - IosMário CarvalhoView Answer on Stackoverflow
Solution 2 - IosSergey MetelinView Answer on Stackoverflow
Solution 3 - IosadamfootdevView Answer on Stackoverflow
Solution 4 - IosJamon HolmgrenView Answer on Stackoverflow
Solution 5 - IosJulian B.View Answer on Stackoverflow
Solution 6 - IosLADView Answer on Stackoverflow
Solution 7 - IosAdam StollerView Answer on Stackoverflow
Solution 8 - IosChowdhury Md Rajib SarwarView Answer on Stackoverflow