How to set margins (padding) in UITextView?

strawtarget picture strawtarget · Jul 12, 2010 · Viewed 49.4k times · Source

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

The contentInset property gives me margins, but it does not change the text "wrap width". The text is wrapped at the same width, and the extra "margin" just causes the view to scroll horizontally.

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

Answer

Karlis picture Karlis · Jan 12, 2014

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)