mattyd mattyd - 5 months ago 163
iOS Question

IOS - remove ALL padding from UITextView

There are many great examples on SO to remove the left padding of a UITextView.

How to lose margin/padding in UITextView?

However, I need to remove the right padding too.

I have tried...

[tv setContentInset: UIEdgeInsetsMake(-4,-8,-8,-X)];//where X is any integer


and just about every other permeation of the last two values to remove the padding and nothing seems to work. Have also tried

[tv sizeToFit];
[tv setTextAlignment:[NSTextAlignmentRight]];


The following Text in the Textview says "00"

enter image description here

Answer

Although it is iOS 7 only, an extremely clean solution is to do the following:

textView.textContainerInset = UIEdgeInsetsZero;
textView.textContainer.lineFragmentPadding = 0;

This will effectively remove all padding (insets) around the text inside the text view. If your deployment target is iOS 7+ then this is the best solution thus far.


For 2016, there is finally, finally, a solution to this absurd Apple problem.

// TextView
// eliminates Apple's ####ing stupid margins from UITextView
import UIKit
@IBDesignable
class TextView:UITextView
    {
    override func prepareForInterfaceBuilder()
        {
        super.prepareForInterfaceBuilder()
        tweak()
        }
    override func awakeFromNib() { tweak() }
    func tweak()
        {
        textContainerInset = UIEdgeInsetsZero;
        textContainer.lineFragmentPadding = 0;
        }
    }

One - Works correctly in storyboard.

Two - Works correctly in builds.

enter image description here

Fortunately it's that simple these days. At last.