class TextViewLabel: UITextView { override init(frame: CGRect, textContainer: NSTextContainer?) { super.init(frame: frame, textContainer: textContainer) commonInit() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() commonInit() } func commonInit() -> Void { isScrollEnabled = false isEditable = false isSelectable = false textContainerInset = UIEdgeInsets.zero textContainer.lineFragmentPadding = 0 } } class TextViewLabelTestVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() let stack: UIStackView = { let v = UIStackView() v.axis = .vertical v.spacing = 8 v.translatesAutoresizingMaskIntoConstraints = false return v }() let v1 = UILabel() let v2 = TextViewLabel() let v3 = UILabel() let v4 = TextViewLabel() [v1, v2, v3, v4].forEach { v in let lb = UILabel() lb.backgroundColor = UIColor(white: 0.95, alpha: 1.0) if let v = v as? UILabel { lb.text = "UILabel" v.backgroundColor = .cyan v.font = .systemFont(ofSize: 16.0) v.numberOfLines = 0 } if let v = v as? TextViewLabel { lb.text = "Text View Label" v.backgroundColor = .green v.font = .systemFont(ofSize: 16.0) } stack.addArrangedSubview(lb) stack.addArrangedSubview(v) } stack.setCustomSpacing(32.0, after: v2) view.addSubview(stack) let g = view.safeAreaLayoutGuide NSLayoutConstraint.activate([ stack.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0), stack.widthAnchor.constraint(equalToConstant: 255.0), stack.centerXAnchor.constraint(equalTo: g.centerXAnchor), ]) var vs: String = "Does this string result in an orphan?" v1.text = vs v2.text = vs vs = "This string should wrap onto four lines of text. Compare the word wrap to see if TextKit allows the orphan." v3.text = vs v4.text = vs } }