UITextField
multiline input
iOS development
Swift programming
UITextView

How to create a multiline UITextfield?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

You cannot truly make UITextField multiline. In UIKit, UITextField is a single-line control by design, so the correct solution for multiline text entry is almost always to use UITextView and style it so it behaves like a text field where needed.

Why UITextField Is the Wrong Control

UITextField is optimized for short, single-line input such as email addresses, names, or search terms. It supports placeholder text, return-key behavior, and common single-line editing flows, but it does not expand to multiple lines of editable content.

If you try to force multiline behavior into it, you usually end up fighting the framework instead of using the control built for the job.

Use UITextView for Multiline Input

The straightforward replacement is UITextView. It supports multiple lines, scrolling, selection, and richer text behavior.

swift
1import UIKit
2
3final class ComposeViewController: 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.layer.borderWidth = 1
13        textView.layer.borderColor = UIColor.systemGray4.cgColor
14        textView.layer.cornerRadius = 8
15        textView.textContainerInset = UIEdgeInsets(top: 12, left: 8, bottom: 12, right: 8)
16
17        view.addSubview(textView)
18
19        NSLayoutConstraint.activate([
20            textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
21            textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
22            textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
23            textView.heightAnchor.constraint(equalToConstant: 160)
24        ])
25    }
26}

That gives you a clean multiline editor with predictable UIKit behavior.

Add Placeholder Behavior Manually

One reason developers reach for UITextField is the built-in placeholder. UITextView does not include that feature, so you add it yourself, usually with a label layered inside the text view.

swift
1import UIKit
2
3final class PlaceholderTextView: UITextView, UITextViewDelegate {
4    private let placeholderLabel = UILabel()
5
6    var placeholder: String = "" {
7        didSet { placeholderLabel.text = placeholder }
8    }
9
10    override init(frame: CGRect, textContainer: NSTextContainer?) {
11        super.init(frame: frame, textContainer: textContainer)
12        delegate = self
13
14        placeholderLabel.textColor = .placeholderText
15        placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
16        addSubview(placeholderLabel)
17
18        NSLayoutConstraint.activate([
19            placeholderLabel.topAnchor.constraint(equalTo: topAnchor, constant: 12),
20            placeholderLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 13)
21        ])
22    }
23
24    required init?(coder: NSCoder) {
25        fatalError("init(coder:) has not been implemented")
26    }
27
28    func textViewDidChange(_ textView: UITextView) {
29        placeholderLabel.isHidden = !text.isEmpty
30    }
31}

This gives you the main usability feature people miss from UITextField.

Match Text-Field Styling

If the goal is visual consistency rather than exact control behavior, style the UITextView to match your form design:

  • apply border and corner radius
  • align fonts with nearby text fields
  • use content insets for comfortable typing
  • disable scrolling if you want the view to grow with content

For chat or notes interfaces, a growing text view often feels better than a fixed-height scrolling editor. That usually means updating a height constraint from contentSize.

When to Use SwiftUI Instead

In SwiftUI, the same rule applies conceptually: use TextField for one line and TextEditor for multiline input. If you are mixing UIKit and SwiftUI, it is still useful to keep the control intent consistent across both frameworks.

That consistency makes validation, keyboard handling, and accessibility behavior easier to reason about.

Common Pitfalls

  • Trying to force UITextField into multiline behavior instead of switching to UITextView.
  • Forgetting that UITextView does not have a built-in placeholder.
  • Leaving default padding and border styles that make the control look visually inconsistent with nearby fields.
  • Enabling scrolling when the design expects the input area to expand with content.
  • Treating multiline input as a visual problem only and ignoring accessibility, keyboard, and validation behavior.

Summary

  • 'UITextField is single-line only and should not be used for real multiline input.'
  • Use UITextView for editable multiline text in UIKit.
  • Add placeholder behavior manually if you need text-field-like UX.
  • Style the UITextView to match the rest of your form instead of fighting the control model.
  • In SwiftUI, the equivalent multiline control is TextEditor, not TextField.

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.