How to set fields (padding) in UITextView?

I have a UITextView for editing text. By default, it has a small text size. I want to increase this margin by a few pixels.

The contentInset property gives me fields, but does not change the text "wrapper width". The text is wrapped with the same width, and the extra “margin” just makes the view scroll horizontally.

Is there a way to make a UITextView of a certain width display text with a narrower "wrapper width"?

+47
objective-c iphone cocoa-touch ipad uitextview
Jul 12 '10 at 20:13
source share
9 answers

Starting with iOS 7, you can use the textContainerInset property:

Objective-c

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

Swift

 textView.textContainerInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
+100
Jan 12 '14 at 3:48
source share

After a lot of work, I found another solution if you are just developing for iOS 6. Set the top and bottom margins using contentInset :

 textView = [[UITextView alloc] init]; textView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0); 

For left and right margins, do not add your plain text at once, but use NSAttributedString , and then correctly set the left and right margins using NSMutableParagraphStyle :

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.headIndent = 20.0; paragraphStyle.firstLineHeadIndent = 20.0; paragraphStyle.tailIndent = -20.0; NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont fontWithName:@"TrebuchetMS" size:12.0], NSParagraphStyleAttributeName: paragraphStyle}; textView.attributedText = [[NSAttributedString alloc] initWithString:myText attributes:attrsDictionary]; 

This gives you a UITextView with your text (in my case from the myText variable) with a 20 pixel fill that scrolls correctly.

+34
Apr 05 '13 at
source share

try it

 UIBezierPath* aObjBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 20)]; txtView.textContainer.exclusionPaths = @[aObjBezierPath]; 
+9
Jul 23 '14 at 9:57
source share

You can simply use the smaller UITextView and put the UIView in the background to simulate padding.

 +----------+ | | <-- UIView (as background) | +--+ | | | | <---- UITextView | | | | | +--+ | | | +----------+ 
+7
Jul 13 2018-10-10T00:
source share

This works for me. Change the contents of the textViewInset and the insert / border of the frame to get the correct fill and wrap words. Then change the borders of the textView to prevent horizontal scrolling. You must also reset the fill every time the text is selected and changed.

 - (void)setPadding { UIEdgeInsets padding = UIEdgeInsetsMake(30, 20, 15, 50); textView.contentInset = padding; CGRect frame = textView.frame; // must change frame before bounds because the text wrap is reformatted based on frame, don't include the top and bottom insets CGRect insetFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(0, padding.left, 0, padding.right)); // offset frame back to original x CGFloat offsetX = frame.origin.x - (insetFrame.origin.x - ( padding.left + padding.right ) / 2); insetFrame = CGRectApplyAffineTransform(insetFrame, CGAffineTransformMakeTranslation(offsetX, 0)); textView.frame = insetFrame; textView.bounds = UIEdgeInsetsInsetRect(textView.bounds, UIEdgeInsetsMake(0, -padding.left, 0, -padding.right)); [textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO]; } -(void)textViewDidChangeSelection:(UITextView *)textView { [self setPadding]; } -(void)textViewDidChange:(UITextView *)textView { [self setPadding]; } 
+1
Jul 12 '12 at 18:29
source share

Thanks for the suggestions. I had this problem while developing a macOS / OSX application and as a result:

 textView.textContainerInset = NSSize(width: 20, height: 20); //Sets margins 
+1
Aug 19 '16 at 8:06
source share

Try entering the code

For iOS7 use textContainerInset

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

Check the link below, which may be useful to you.

stack overflow

0
Sep 04 '17 at 11:46 on
source share

Swift + iOS 12 : a different approach

Setting the UITextView left + right of our UITextView really allowed the text to disappear when more than 9 lines of text were entered. Using the @rajesh solution helped solve this problem:

Be sure to call the method whenever the text has changed + the device rotates.

  /// Updates the left + right padding of the current text view. /// -> leftRightPadding value = 11.0 func updateLeftRightPadding() { let leftPadding = UIBezierPath(rect: .init(x: 0.0, y: 0.0, width: leftRightPadding, height: contentSize.height)) let rightPadding = UIBezierPath(rect: .init(x: frame.width - leftRightPadding, y: 0.0, width: 11, height: contentSize.height)) textContainer.exclusionPaths = [leftPadding, rightPadding] } 
0
11 Oct '18 at 20:03
source share

if you want to indent on one side, you can use the following code:

  textView.contentInset.left = 5 //For left padding textView.contentInset.right = 5 //For right padding textView.contentInset.top = 5 //For top padding textView.contentInset.bottom = 5 //For bottom padding 
0
Dec 10 '18 at 7:43
source share



All Articles