open class CodeCell: UITableViewCell { class CodeScrollView: UIScrollView, UIGestureRecognizerDelegate { func gestureRecognizer( _ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer ) -> Bool { return true } } let scrollView = CodeScrollView() private let textView: UITextView private let textViewLayoutManager: CodeLayoutManager private let container = NSTextContainer(size: .zero) public var textViewBackgroundColor: UIColor? { get { textView.backgroundColor } set { textView.backgroundColor = newValue } } private var textContainerInset: UIEdgeInsets { get { textView.textContainerInset} set { textView.textContainerInset = newValue textViewLayoutManager.textContainerInset = newValue } } override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { textViewLayoutManager = CodeLayoutManager() textViewLayoutManager.addTextContainer(container) let storage = NSTextStorage() storage.addLayoutManager(textViewLayoutManager) textView = UITextView(frame: .zero, textContainer: container) super.init(style: style, reuseIdentifier: reuseIdentifier) selectionStyle = .none setUpViews() } required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setUpViews() { textView.textContainer.lineFragmentPadding = 0 textView.isEditable = false textView.translatesAutoresizingMaskIntoConstraints = false textView.isScrollEnabled = false textView.scrollsToTop = false textView.backgroundColor = .clear contentView.addSubview(scrollView) scrollView.addSubview(textView) scrollView.translatesAutoresizingMaskIntoConstraints = false textView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ textView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor), textView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor), textView.topAnchor.constraint(equalTo: scrollView.topAnchor), textView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor), scrollView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), scrollView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), scrollView.topAnchor.constraint(equalTo: contentView.topAnchor), scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), scrollView.heightAnchor.constraint(equalTo: textView.heightAnchor) ]) } }