@@ -16,6 +16,7 @@ public protocol MessageTextViewListener: AnyObject {
1616open class MessageTextView : UITextView , UITextViewDelegate {
1717
1818 internal let placeholderLabel = UILabel ( )
19+ internal let trailingButton = ExpandedHitTestButton ( type: . system)
1920 internal var listeners : NSHashTable < AnyObject > = NSHashTable . weakObjects ( )
2021
2122 open override var delegate : UITextViewDelegate ? {
@@ -78,6 +79,15 @@ open class MessageTextView: UITextView, UITextViewDelegate {
7879 }
7980 }
8081
82+ public var buttonTintColor : UIColor {
83+ get { return trailingButton. tintColor }
84+ set {
85+ trailingButton. tintColor = newValue
86+ trailingButton. setTitleColor ( newValue, for: . normal)
87+ trailingButton. imageView? . tintColor = newValue
88+ }
89+ }
90+
8191 public override init ( frame: CGRect , textContainer: NSTextContainer ? ) {
8292 super. init ( frame: frame, textContainer: textContainer)
8393 commonInit ( )
@@ -108,6 +118,27 @@ open class MessageTextView: UITextView, UITextViewDelegate {
108118 set { placeholderLabel. textColor = newValue }
109119 }
110120
121+ public func setButton( title: String ? ) {
122+ trailingButton. setTitle ( title, for: . normal)
123+ if #available( iOS 13 . 0 , * ) {
124+ trailingButton. setTitleColor ( . label, for: . normal)
125+ } else {
126+ trailingButton. setTitleColor ( . black, for: . normal)
127+ }
128+ trailingButton. sizeToFit ( )
129+ setNeedsLayout ( )
130+ }
131+
132+ public func setButton( image: UIImage ? ) {
133+ trailingButton. setImage ( image, for: . normal)
134+ trailingButton. sizeToFit ( )
135+ setNeedsLayout ( )
136+ }
137+
138+ public func addButton( target: Any , action: Selector ) {
139+ trailingButton. addTarget ( target, action: action, for: . touchUpInside)
140+ }
141+
111142 // MARK: Overrides
112143
113144 open override func layoutSubviews( ) {
@@ -120,6 +151,18 @@ open class MessageTextView: UITextView, UITextViewDelegate {
120151 width: placeholderSize. width,
121152 height: placeholderSize. height
122153 )
154+
155+ // adjust by bottom offset so content is flush w/ text view
156+ let rightButtonSize = trailingButton. bounds. size
157+ let rightButtonFrame = CGRect (
158+ x: bounds. maxX - rightButtonSize. width - 8 ,
159+ y: bounds. maxY - rightButtonSize. height - 8 ,
160+ width: rightButtonSize. width,
161+ height: rightButtonSize. height
162+ )
163+ trailingButton. frame = rightButtonFrame
164+
165+ textContainerInset. right = rightButtonFrame. width + 8
123166 }
124167
125168 // MARK: Private API
@@ -134,6 +177,8 @@ open class MessageTextView: UITextView, UITextViewDelegate {
134177
135178 defaultTextAttributes [ NSAttributedString . Key. font] = defaultFont
136179 defaultTextAttributes [ NSAttributedString . Key. foregroundColor] = defaultTextColor
180+
181+ addSubview ( trailingButton)
137182 }
138183
139184 internal func enumerateListeners( block: ( MessageTextViewListener ) -> Void ) {
0 commit comments