forked from GitHawkApp/MessageViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
101 lines (80 loc) · 3.96 KB
/
ViewController.swift
File metadata and controls
101 lines (80 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// ViewController.swift
// Examples
//
// Created by Ryan Nystrom on 1/28/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import UIKit
import MessageViewController
class ViewController: MessageViewController, UITableViewDataSource, UITableViewDelegate, MessageAutocompleteControllerDelegate {
var data = "Lorem ipsum dolor sit amet|consectetur adipiscing elit|sed do eiusmod|tempor incididunt|ut labore et dolore|magna aliqua| Ut enim ad minim|veniam, quis nostrud|exercitation ullamco|laboris nisi ut aliquip|ex ea commodo consequat|Duis aute|irure dolor in reprehenderit|in voluptate|velit esse cillum|dolore eu|fugiat nulla pariatur|Excepteur sint occaecat|cupidatat non proident|sunt in culpa|qui officia|deserunt|mollit anim id est laborum"
.components(separatedBy: "|")
let users = ["rnystrom", "BasThomas", "jessesquires", "Sherlouk"]
var autocompleteUsers = [String]()
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
view.addSubview(tableView)
borderColor = .lightGray
messageView.inset = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
messageView.textView.placeholderText = "New message..."
messageView.textView.placeholderTextColor = .lightGray
messageView.font = UIFont.systemFont(ofSize: 17)
messageView.set(buttonTitle: "Send", for: .normal)
messageView.addButton(target: self, action: #selector(onButton))
messageView.buttonTint = .blue
messageAutocompleteController.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
messageAutocompleteController.tableView.dataSource = self
messageAutocompleteController.tableView.delegate = self
messageAutocompleteController.register(prefix: "@")
// Set custom attributes for an autocompleted string
let tintColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
messageAutocompleteController.registerAutocomplete(prefix: "@", attributes: [
.font: UIFont.preferredFont(forTextStyle: .body),
.foregroundColor: tintColor,
.backgroundColor: tintColor.withAlphaComponent(0.1)
])
messageAutocompleteController.delegate = self
setup(scrollView: tableView)
}
@objc func onButton() {
data.append(messageView.text)
messageView.text = ""
tableView.reloadData()
tableView.scrollToRow(
at: IndexPath(row: data.count - 1, section: 0),
at: .bottom,
animated: true
)
}
// MARK: UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableView === self.tableView
? data.count
: autocompleteUsers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if tableView === self.tableView {
cell.textLabel?.text = data[indexPath.row]
} else {
cell.textLabel?.text = autocompleteUsers[indexPath.row]
}
return cell
}
// MARK: UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if tableView === messageAutocompleteController.tableView {
messageAutocompleteController.accept(autocomplete: autocompleteUsers[indexPath.row])
}
}
// MARK: MessageAutocompleteControllerDelegate
func didFind(controller: MessageAutocompleteController, prefix: String, word: String) {
autocompleteUsers = users.filter { word.isEmpty || $0.lowercased().contains(word.lowercased()) }
controller.show(true)
}
}