Skip to content

Instantly share code, notes, and snippets.

@raygun101
Last active February 9, 2022 06:11
Show Gist options
  • Select an option

  • Save raygun101/3da0382f798e8f1d0b68fea66a6f539d to your computer and use it in GitHub Desktop.

Select an option

Save raygun101/3da0382f798e8f1d0b68fea66a6f539d to your computer and use it in GitHub Desktop.

Revisions

  1. raygun101 revised this gist Feb 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UITextViewWithAutoLayout.swift
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ import UIKit
    ///
    /// 🍰 Layered Cakewalk - UITextViewWithAutoLayout
    ///
    /// A UITextView that has a `intrinsicContentSize` equal to the content size.
    /// A UITextView that has an `intrinsicContentSize` equal to the content size.
    ///

    class UITextViewWithAutoLayout : UITextView
  2. raygun101 revised this gist Feb 9, 2022. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions UITextViewWithAutoLayout.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    import UIKit

    ///
    /// 🍰 Layered Cakewalk - UITextViewWithAutoLayout
    ///
    /// A UITextView that has a `intrinsicContentSize` equal to the content size.
    ///

    class UITextViewWithAutoLayout : UITextView
    {
  3. raygun101 revised this gist Feb 9, 2022. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions UITextViewWithAutoLayout.swift
    Original file line number Diff line number Diff line change
    @@ -19,16 +19,19 @@ class UITextViewWithAutoLayout : UITextView
    self.textContainerInset = .zero
    self.textContainer.lineFragmentPadding = 0

    _observer = self.observe(\.contentSize)
    _observer = self.observe(\.contentSize, options: [.old, .new])
    {
    view, change in

    view.invalidateIntrinsicContentSize()
    if change.oldValue != change.newValue
    {
    view.invalidateIntrinsicContentSize()
    }
    }
    }

    override var intrinsicContentSize: CGSize
    {
    self.sizeThatFits(CGSize(.greatestFiniteMagnitude))
    self.contentSize
    }
    }
  4. raygun101 revised this gist Jan 20, 2022. No changes.
  5. raygun101 revised this gist Jan 20, 2022. No changes.
  6. raygun101 created this gist Jan 20, 2022.
    34 changes: 34 additions & 0 deletions UITextViewWithAutoLayout.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import UIKit


    class UITextViewWithAutoLayout : UITextView
    {
    required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

    private var _observer: NSObjectProtocol?
    ///
    override init(frame: CGRect, textContainer: NSTextContainer?)
    {
    super.init(frame: frame, textContainer: textContainer)

    self.enablesReturnKeyAutomatically = true
    self.backgroundColor = .clear
    self.layoutMargins = .zero
    self.insetsLayoutMarginsFromSafeArea = false
    self.contentInset = .zero
    self.textContainerInset = .zero
    self.textContainer.lineFragmentPadding = 0

    _observer = self.observe(\.contentSize)
    {
    view, change in

    view.invalidateIntrinsicContentSize()
    }
    }

    override var intrinsicContentSize: CGSize
    {
    self.sizeThatFits(CGSize(.greatestFiniteMagnitude))
    }
    }