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.
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.
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:
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.
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.
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:
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
textContainerInsetbut forgettingtextContainer.lineFragmentPadding, which leaves horizontal padding in place. - Confusing parent layout margins or stack view spacing with actual
UITextViewpadding. - 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, andcontentInset. - 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
- iOS - Swift - Function that returns asynchronously retrieved value
- iOS - UIImageView - how to handle UIImage image orientation
- iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash
- iOS 10 App if we're in the real pre-commit handler we can't actually add any new fences due to CA restriction
- iOS 10 doesn't print NSLogs
- iOS 10 error access private when using UIImagePickerController
- iOS 10 GM release error when submitting apps app attempts to access privacy-sensitive data without a usage description due to GoogleSignIn, AdMob
- iOS 11 - Keyboard Height is returning 0 in keyboard notification
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.