Swift 4: Align TextView with UILabel

Lawrence Tan
1 min readNov 9, 2017

--

This is a very short example of how to align the contents of both views as UITextView comes with a natural “padding” that will offset the contents off its frame:

Before

Create a new custom TextView class and override draw.

class SLTextView: UITextView {    
override func draw(_ rect: CGRect) {
textContainer.lineFragmentPadding = 0;
textContainerInset = .zero;
}}
After

and … there you go! Nicely aligned text and label.

Find this useful? Clap your 👏 to say “thanks!” and also to help spread this around!.

--

--

Lawrence Tan

Googler. Loves God, Loves my wife, Loves my Family & Corgis.