Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add "padding" to a UITextView

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?

like image 304
magtak Avatar asked Oct 26 '11 13:10

magtak


People also ask

How do I add padding to textfield?

A JTextField class will generate an ActionListener interface when we trying to enter some input inside it. The important methods of a JTextField class are setText(), getText(), setBorder(), setEnabled(), etc. We can add padding to a JTextField using the setMargin(Insets s) of JTextComponent class.

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


2 Answers

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

like image 132
Mário Carvalho Avatar answered Sep 28 '22 18:09

Mário Carvalho


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);  
like image 22
Sergey Metelin Avatar answered Sep 28 '22 18:09

Sergey Metelin