|ReleasedLast Release||Mar 2017|
Maintained by Vlas Voloshin.
|SizeIntegration Size||36 kb|
|LOCLines of Code||222|
SubviewAttachingTextView is a
UITextView subclass that allows embedding subviews in its attributed text as text attachments. The library also provides a
SubviewAttachingTextViewBehavior class that allows compositing the attachment behavior in existing
SubviewAttachingTextView in action, clone or download this repository and check out
SubviewAttachingTextView is built on top of
NSTextAttachment and Text Kit APIs available through
UITextView. A custom subclass of
NSTextAttachment allows keeping a reference to the embedded views, and text view’s
NSTextContainer allow querying for the attachments’ bounds as they’re laid out inside the text, taking into account all paragraph style properties.
NSTextStorage’s delegate callbacks allow implementing automatic layout and attachment updates.
SubviewAttachingTextView.xcodeprojinto your project or workspace in Xcode.
Your Target, you might find that Xcode has added a missing framework item in “Embedded Binaries”. Delete it for now.
SubviewAttachingTextView.frameworkto “Embedded Binaries”. This should also add it to “Linked Frameworks and Libraries”.
There are two ways to integrate the subview attachment behavior in your text views; choose the one that fits your needs:
Simply change the class of
UITextView you use to
SubviewAttachingTextView (in Swift code) or
VVSubviewAttachingTextView (in Objective-C code and Interface Builder).
You can easily integrate
SubviewAttachingTextViewBehavior class into your custom
UITextView subclass by following the implementation of
init?(coder:)initializers, create a
VVSubviewAttachingTextViewBehaviorin Objective-C), store it in your text view instance.
textViewproperty. It’s a weak property, so this won’t create a retain cycle.
textStorage.delegate. This will enable the automatic attachment management.
layoutManager(_:, didCompleteLayoutFor:, atEnd:)and
textStorage(_:, didProcessEditing:, range:, changeInLength:)delegate methods, but keep in mind that
SubviewAttachingTextViewBehaviormight require more methods in future.
textContainerInsetproperty of your text view and make it call
layoutAttachedSubviews()method of the attachment behavior. This ensures that the layout of attachments is updated when container insets are changed.
To embed a view in your text, do the following:
VVSubviewTextAttachmentin Objective-C) and initialize it with a view object, optionally providing a size for the attachment.
VVTextAttachedViewProviderin Objective-C). Using a view provider allows rendering the attributed string with an attachment in multiple text views at the same time, with each text view receiving its own instance of a view representing the attachment from the view provider.
TextAttachedViewProvideralso allows customizing the attached view’s size during text layout.
NSAttributedStringfrom the attachment using its
SubviewAttachingTextViewor your custom text view embedding a
This library is available under the MIT license. See the
LICENSE file for more info.