iOS
UITextView
padding
UI customization
app development

IOS - remove ALL padding from UITextView

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

UITextView adds more internal spacing than many developers expect. If you only clear one inset value, the text still appears shifted, which makes the control hard to align with labels, icons, or custom borders. The fix is straightforward once you know which properties affect layout.

Why UITextView Still Looks Padded

The visible gap around text usually comes from three different places:

  • 'textContainerInset'
  • 'textContainer.lineFragmentPadding'
  • 'contentInset'

textContainerInset adds space around the entire text container. lineFragmentPadding adds left and right margin inside each rendered line. contentInset belongs to the underlying scroll view behavior and can still affect the visible content area.

This is why many developers set textContainerInset = .zero and still see indentation. The left and right padding usually remain because lineFragmentPadding is still nonzero.

Remove All Internal Padding in UIKit

For most UIKit screens, reset all three values together. That gives you a text view whose text starts exactly where the text container starts.

swift
1import UIKit
2
3final class NoteViewController: UIViewController {
4    private let textView = UITextView()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        view.backgroundColor = .systemBackground
9
10        textView.translatesAutoresizingMaskIntoConstraints = false
11        textView.font = .systemFont(ofSize: 16)
12        textView.backgroundColor = .secondarySystemBackground
13        textView.text = "This text starts at the visible edge."
14
15        textView.textContainerInset = .zero
16        textView.textContainer.lineFragmentPadding = 0
17        textView.contentInset = .zero
18        textView.scrollIndicatorInsets = .zero
19
20        view.addSubview(textView)
21        NSLayoutConstraint.activate([
22            textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
23            textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
24            textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 24),
25            textView.heightAnchor.constraint(equalToConstant: 120)
26        ])
27    }
28}

This removes internal spacing without changing the external Auto Layout constraints. Your view can still keep a 16-point margin from the screen edge while the text itself sits flush within the text view’s bounds.

Use a Subclass for Consistency

If you need the same behavior in several screens, wrap it in a subclass instead of repeating the setup everywhere:

swift
1import UIKit
2
3final class EdgeToEdgeTextView: UITextView {
4    override func didMoveToWindow() {
5        super.didMoveToWindow()
6        textContainerInset = .zero
7        textContainer.lineFragmentPadding = 0
8        contentInset = .zero
9        scrollIndicatorInsets = .zero
10    }
11}

This approach is useful in design systems because the padding policy lives in one place. If you later decide to restore a small vertical inset for readability, you only change the subclass.

Auto-Growing Text Views Need Extra Care

Padding bugs are often confused with sizing bugs. When a text view grows with its content, clipping can make the top or bottom feel padded even though the problem is really the height constraint.

swift
1import UIKit
2
3func updateHeight(for textView: UITextView, heightConstraint: NSLayoutConstraint) {
4    textView.isScrollEnabled = false
5    let targetSize = CGSize(width: textView.bounds.width, height: .greatestFiniteMagnitude)
6    let fittingSize = textView.sizeThatFits(targetSize)
7    heightConstraint.constant = fittingSize.height
8}

If the view is not scrollable, update the height after the width is known. Otherwise the content can wrap differently than expected and create the impression that internal spacing still exists.

Attributed Text Can Add Its Own Indent

Another common surprise is attributed text. You can clear every inset on the view and still see the first line start later than expected because the paragraph style carries its own indentation values.

swift
1import UIKit
2
3let style = NSMutableParagraphStyle()
4style.firstLineHeadIndent = 0
5style.headIndent = 0
6style.tailIndent = 0
7
8let text = NSAttributedString(
9    string: "Aligned text without paragraph indent.",
10    attributes: [.paragraphStyle: style]
11)

If content comes from HTML, rich text import, or a Markdown pipeline, inspect the paragraph attributes before changing layout code again. In many cases the text view is already configured correctly.

SwiftUI Wrapper Example

The same UIKit properties matter when UITextView is hosted inside SwiftUI with UIViewRepresentable:

swift
1import SwiftUI
2import UIKit
3
4struct TightTextView: UIViewRepresentable {
5    @Binding var text: String
6
7    func makeUIView(context: Context) -> UITextView {
8        let view = UITextView()
9        view.textContainerInset = .zero
10        view.textContainer.lineFragmentPadding = 0
11        view.contentInset = .zero
12        return view
13    }
14
15    func updateUIView(_ uiView: UITextView, context: Context) {
16        if uiView.text != text {
17            uiView.text = text
18        }
19    }
20}

The important detail is to apply the zero-padding configuration at creation time so SwiftUI does not start from the default UIKit layout.

Common Pitfalls

  • Resetting textContainerInset but forgetting textContainer.lineFragmentPadding, which leaves horizontal padding in place.
  • Confusing parent layout margins or stack view spacing with actual UITextView padding.
  • Removing insets once and then overriding them later in another setup method or style pass.
  • Ignoring paragraph style indentation in attributed content.
  • Disabling scrolling on a growing text view without updating its height, which makes clipping look like leftover padding.

Summary

  • Full padding removal usually means clearing textContainerInset, lineFragmentPadding, and contentInset.
  • Reuse a helper or subclass so every screen gets the same text view behavior.
  • Check sizing and constraints before assuming the padding settings failed.
  • Inspect attributed text when alignment still looks wrong.
  • Treat internal text padding and external layout margins as separate problems.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.