Skip to content

Instantly share code, notes, and snippets.

@abbeycode
Created January 3, 2018 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abbeycode/b40ade1f23a8946a12ad816e1bc0f2b1 to your computer and use it in GitHub Desktop.
Save abbeycode/b40ade1f23a8946a12ad816e1bc0f2b1 to your computer and use it in GitHub Desktop.
TextViewWrap.swift
import UIKit
import PlaygroundSupport
class TestViewController : UIViewController {
let btn: UIButton = {
let b = UIButton()
b.translatesAutoresizingMaskIntoConstraints = false
b.setTitle("Tap Me", for: .normal)
b.backgroundColor = .red
return b
}()
let scrollView: UIScrollView = {
let v = UIScrollView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .blue
return v
}()
let scrollContentView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .orange
return v
}()
let stackView: UIStackView = {
let v = UIStackView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .green
v.axis = .vertical
v.alignment = .fill
v.distribution = .fill
v.spacing = 8.0
return v
}()
let overviewTextView: UITextView = {
let v = UITextView()
v.translatesAutoresizingMaskIntoConstraints = false
v.isScrollEnabled = false
v.backgroundColor = .yellow
v.font = UIFont.systemFont(ofSize: 28.0)
return v
}()
let labelBelow: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .cyan
v.text = "Label below"
v.font = UIFont.systemFont(ofSize: 28.0)
return v
}()
var imageRect = CGRect(x: 0, y: 0, width: 140, height: 240)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
scrollView.addSubview(scrollContentView)
scrollContentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
scrollContentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
scrollContentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
scrollContentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
let scrollContentBottomConstraint = scrollContentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
scrollContentBottomConstraint.priority = .defaultLow
scrollContentBottomConstraint.isActive = true
scrollContentView.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: scrollContentView.topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: scrollContentView.safeAreaLayoutGuide.leadingAnchor, constant: 8).isActive = true
stackView.trailingAnchor.constraint(equalTo: scrollContentView.safeAreaLayoutGuide.trailingAnchor, constant: -8).isActive = true
stackView.setContentHuggingPriority(.defaultLow, for: .horizontal)
stackView.setContentHuggingPriority(.defaultLow, for: .vertical)
stackView.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
stackView.setContentCompressionResistancePriority(.required, for: .vertical)
let stackBottomConstraint = stackView.bottomAnchor.constraint(equalTo: scrollContentView.bottomAnchor)
stackBottomConstraint.priority = .defaultLow
stackBottomConstraint.isActive = true
stackView.addArrangedSubview(btn)
btn.addTarget(self, action: #selector(didTap(_:)), for: .touchUpInside)
btn.heightAnchor.constraint(equalToConstant: 40).isActive = true
stackView.addArrangedSubview(overviewTextView)
overviewTextView.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true
overviewTextView.setContentHuggingPriority(.defaultLow, for: .horizontal)
overviewTextView.setContentHuggingPriority(.defaultHigh, for: .vertical)
overviewTextView.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
overviewTextView.setContentCompressionResistancePriority(.required, for: .vertical)
overviewTextView.text = "This is working fine to wrap the text, but here's the problem: I have the text view (lorem ipsum below) in a UIStackView with scrolling disabled, and I have items beneath it."
stackView.addArrangedSubview(labelBelow)
labelBelow.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true
}
@objc
func didTap(_ sender: Any?) -> Void {
if overviewTextView.textContainer.exclusionPaths.first != nil {
overviewTextView.textContainer.exclusionPaths = []
} else {
let imageBezierPath = UIBezierPath(rect: imageRect)
overviewTextView.textContainer.exclusionPaths = [imageBezierPath]
}
view.setNeedsLayout()
view.layoutIfNeeded()
}
}
let vc = TestViewController()
vc.view.backgroundColor = .white
PlaygroundPage.current.liveView = vc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment