Skip to content

Commit b030168

Browse files
committed
add support to add button inside textview
1 parent 8d01e56 commit b030168

6 files changed

Lines changed: 80 additions & 3 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info" : {
3-
"version" : 1,
4-
"author" : "xcode"
3+
"author" : "xcode",
4+
"version" : 1
55
}
6-
}
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "ai-text-generation.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"filename" : "ai-text-generation@2x.png",
10+
"idiom" : "universal",
11+
"scale" : "2x"
12+
},
13+
{
14+
"idiom" : "universal",
15+
"scale" : "3x"
16+
}
17+
],
18+
"info" : {
19+
"author" : "xcode",
20+
"version" : 1
21+
}
22+
}
634 Bytes
Loading
484 Bytes
Loading

Examples/Examples/ViewController.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class ViewController: MessageViewController, UITableViewDataSource, UITableViewD
3939
messageView.textView.placeholderText = "New message..."
4040
messageView.textView.placeholderTextColor = .lightGray
4141

42+
messageView.textView.addButton(target: self, action: #selector(onLeftButton))
43+
let image = UIImage(named: "ai-text-generation")?.withRenderingMode(.alwaysTemplate)
44+
messageView.textView.setButton(image: image)
45+
messageView.textView.buttonTintColor = .blue
46+
47+
messageView.textView.layer.borderWidth = 1.0
48+
messageView.textView.layer.borderColor = UIColor.gray.cgColor
49+
messageView.textView.layer.cornerRadius = 16.0
50+
messageView.textView.layer.masksToBounds = true
51+
4252
messageView.setButton(title: "Send", for: .normal, position: .right)
4353
messageView.addButton(target: self, action: #selector(onRightButton), position: .right)
4454
messageView.rightButtonTint = .blue

Sources/MessageViewController/MessageTextView.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public protocol MessageTextViewListener: AnyObject {
1616
open 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

Comments
 (0)