Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set margins (padding) in UITextView?

People also ask

How do I set padding in Xcode?

These methods just allow you to add a rect (frame) to your textfield's label. newBounds method create the rect (frame) that will be added to textfield's label. Finally your padding is: let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);

How do I add padding to textfield?

We can add padding to a JTextField using the setMargin(Insets s) of JTextComponent class.

How do I change the color of a link in TextView Swift?

You can Change the Hyperlink Color in a TextView by the following: In the Nib file, you can go to the Properties Window and change the Tint to which ever color you want to.


Starting from iOS 7 you can use textContainerInset property:

Objective-C

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

Swift

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

After fiddling around with this for a while I found another solution if you're only developing for iOS 6. Set the top and bottom margins with contentInset:

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

For the left and right margins don't add your plain text right away but use an NSAttributedString instead with properly set left and right indents with an 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 variable myText) with 20 pixels padding that properly scrolls.


Try this

UIBezierPath* aObjBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 20)];
txtView.textContainer.exclusionPaths  = @[aObjBezierPath];

You could just use a smaller UITextView, and place a UIView in the background to simulate the padding.

+----------+
|          | <-- UIView (as background)
|   +--+   |
|   |  | <---- UITextView
|   |  |   |
|   +--+   |
|          |
+----------+

if you want to set padding from one side, you can use below 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