How to lose margin/padding in UITextView

Viewed 223521

I have a UITextView in my iOS application, which displays a large amount of text.

I am then paging this text by using the offset margin parameter of the UITextView.

My problem is that the padding of the UITextView is confusing my calculations as it seems to be different depending on the font size and typeface that I use.

Is it possible to remove the padding surrounding the content of the UITextView?

24 Answers

This workaround was written in 2009 when iOS 3.0 was released. It no longer applies.

I ran into the exact same problem, and in the end I had to wind up using

nameField.contentInset = UIEdgeInsetsMake(-4, -8, 0, 0);

where nameField is a UITextView. The font I happened to be using was Helvetica 16 point. It’s only a custom solution for the particular field size I was drawing. This makes the left offset flush with the left side, and the top offset where I want it for the box it’s drawn in.

In addition, this only seems to apply to UITextViews where you are using the default aligment, i.e.,

nameField.textAlignment = NSTextAlignmentLeft;

Align to the right for example and the UIEdgeInsetsMake seems to have no impact on the right edge at all.

At very least, using the .contentInset property allows you to place your fields with the "correct" positions, and accommodate the deviations without offsetting your UITextViews.

For iOS 10, the following line works for the top and bottom padding removing.

captionTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)

Latest Swift:

self.textView.textContainerInset = .init(top: -2, left: 0, bottom: 0, right: 0)
self.textView.textContainer.lineFragmentPadding = 0

For Swift 4, Xcode 9

Use the following function. It can change the margin/padding of the text in UITextView:

public func UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsets

So in this case it is:

 self.textView?.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)

For SwiftUI

If you are making your own TextView using UIViewRepresentable and want to control the padding, in your makeUIView function, simply do:

uiTextView.textContainerInset = UIEdgeInsets(top: 10, left: 18, bottom: 0, right: 18)

or whatever you want.

For me (iOS 11 & Xcode 9.4.1) what worked magically was setting up textView.font property to UIFont.preferred(forTextStyle:UIFontTextStyle) style and also the first answer as mentioned by Fattie. But the Fattie answer did not work till I set the textView.font property. Else UITextView keeps behaving erratically.

In case anyone is looking for the latest Swift version then the below code is working fine with Xcode 10.2 and Swift 4.2

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

In case you want to set an HTML string and avoid the bottom padding, please make sure that you are not using block tags, i.e., div and p.

In my case this was the reason. You can easily test it out by replacing occurrences of block tags with, e.g., the span tag.

You need to set inset and lineFragmentPadding on didMoveToSuperView

@IBDesignable class UITextViewFixed: UITextView {
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        setup()
    }
    func setup() {
        textContainerInset = UIEdgeInsets.zero
        textContainer.lineFragmentPadding = 0
    }
}

For anyone coming to this because of parsing HTML into UITextView, just remove the new line introduced by the closing p tag "/n". I assume that you work with NSMutableAttributedString:

if let lastCharacter = attributedString.string.last, lastCharacter == "\n" {
    attributedString.deleteCharacters(in: NSRange(location:(attributedString.length) - 1, length:1))
}
Related