-
Notifications
You must be signed in to change notification settings - Fork 6
/
WTextView.swift
327 lines (267 loc) · 10.6 KB
/
WTextView.swift
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// WTextView.swift
// WMobileKit
//
// Copyright 2017 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
import SnapKit
let KEY_RANGE_TOTAL = "TotalRange"
let KEY_RANGE_LINK = "LinkRange"
let KEY_RANGE_ADDRESS = "AddressRange"
public protocol WTextViewDelegate: UITextViewDelegate {
func textViewShouldReturn(_ textView: WTextView) -> Bool
}
open class WTextView: UITextView, UITextViewDelegate {
let leftImageView: UIImageView = UIImageView()
let placeholderLabel: UILabel = UILabel()
let testLabel: UILabel = UILabel()
open weak var wTextViewDelegate: WTextViewDelegate?
override open var text: String! {
didSet {
textDidChange()
}
}
override open var attributedText: NSAttributedString! {
didSet {
textDidChange()
}
}
open var placeholderText: String = "" {
didSet {
placeholderLabel.text = placeholderText
textDidChange()
}
}
open var placeholderTextColor: UIColor = UIColor(red: 0.0, green: 0.0, blue: 0.0980392, alpha: 0.22) {
didSet {
placeholderLabel.textColor = placeholderTextColor
textDidChange()
}
}
open var leftImage: UIImage? {
didSet {
leftImageView.image = leftImage
updateUI()
}
}
override open var font: UIFont? {
didSet {
placeholderLabel.font = font
textDidChange()
}
}
override open var textAlignment: NSTextAlignment {
didSet {
placeholderLabel.textAlignment = textAlignment
textDidChange()
}
}
open var verticalOffsetForLeftImage: CGFloat = 0 {
didSet {
updateUI()
}
}
open var leftPaddingForLeftImage: CGFloat = 4 {
didSet {
updateUI()
}
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
public override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
commonInit()
}
public convenience init(_ text: String) {
self.init(frame: CGRect.zero)
commonInit()
}
open override func didMoveToSuperview() {
super.didMoveToSuperview()
updateUI()
}
open override func becomeFirstResponder() -> Bool {
let returnValue = super.becomeFirstResponder()
updateUI()
return returnValue
}
open func commonInit() {
isEditable = false
isScrollEnabled = false
delegate = self
NotificationCenter.default.addObserver(self,
selector: #selector(textDidChange),
name: NSNotification.Name.UITextViewTextDidChange,
object: nil)
addSubview(leftImageView)
addSubview(placeholderLabel)
// This is a workaround to get the default font set on the text view
// the property is explicitly unwrapped but nil during initialization until text is set
let oldText = text
text = "a"
text = oldText
placeholderLabel.text = placeholderText
placeholderLabel.numberOfLines = 0
placeholderLabel.backgroundColor = UIColor.clear
placeholderLabel.isHidden = !text.isEmpty
}
@objc fileprivate func textDidChange() {
updateUI()
}
fileprivate func updateUI() {
placeholderLabel.font = font
placeholderLabel.textColor = placeholderTextColor
placeholderLabel.textAlignment = textAlignment
placeholderLabel.isHidden = !text.isEmpty
if (self.superview != nil) {
let imageWidthHeight = 20
let leftInset: CGFloat = leftImageView.image != nil ? CGFloat(imageWidthHeight) + leftPaddingForLeftImage - 2 : 0
let leftPlaceholderOffset = 5 + leftInset
textContainerInset = UIEdgeInsets(top: 8, left: leftInset, bottom: 8, right: 0)
leftImageView.snp.remakeConstraints() { (make) in
make.centerY.equalTo(self).offset(verticalOffsetForLeftImage)
make.left.equalTo(self).offset(leftPaddingForLeftImage)
make.width.equalTo(imageWidthHeight)
make.height.equalTo(imageWidthHeight)
}
placeholderLabel.snp.remakeConstraints() { (make) in
make.centerY.equalTo(self).offset(0.5)
make.width.equalTo(self).offset(-leftPlaceholderOffset - 5).priority(750)
make.left.equalTo(self).offset(leftPlaceholderOffset)
make.height.equalTo(self)
}
layoutIfNeeded()
}
}
open func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if let delegate = wTextViewDelegate , text == "\n" {
return delegate.textViewShouldReturn(self)
}
return true
}
deinit {
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.UITextViewTextDidChange,
object: nil)
}
}
open class WMarkdownTextView: WTextView {
override open var text: String! {
didSet {
textDidChange()
buildTextView(text)
}
}
public convenience init(_ text: String) {
self.init(frame: CGRect.zero)
commonInit()
buildTextView(text)
}
open func buildTextView(_ text: String) {
let attribString = NSMutableAttributedString(string: text)
if let markdownArray = rangeForMarkdownURL(text) {
for markdownDict in markdownArray.reversed() {
let totalRange = markdownDict[KEY_RANGE_TOTAL]
let linkRange = markdownDict[KEY_RANGE_LINK]
let addressRange = markdownDict[KEY_RANGE_ADDRESS]
let linkString = text.substring(with: linkRange!)
let addressString = text.substring(with: addressRange!)
let startIndexDistance = text.characters.distance(from: text.startIndex, to: totalRange!.lowerBound)
let replaceRange = NSMakeRange(startIndexDistance, text.characters.distance(from: text.startIndex, to: totalRange!.upperBound) - startIndexDistance)
let replacedAttribString = NSMutableAttributedString(string: linkString)
replacedAttribString.addAttribute(NSAttributedStringKey.link, value: addressString, range: NSMakeRange(0, linkString.characters.count))
attribString.replaceCharacters(in: replaceRange, with: replacedAttribString)
}
}
self.attributedText = attribString
}
open func rangeForMarkdownURL(_ text: String) -> Array<Dictionary<String, Range<String.Index>>>? {
var numOpeningBrackets = 0
var openBracketPos, closeBracketPos, openParenPos, closeParenPos: Int?
var foundURLString = false
var markdownArray = [Dictionary<String, Range<String.Index>>]()
let resetVariables: () -> () = {
openBracketPos = nil
closeBracketPos = nil
openParenPos = nil
closeParenPos = nil
numOpeningBrackets = 0
foundURLString = false
}
// find end of url string
for (index, char) in text.characters.enumerated() {
switch char {
case "[":
if (foundURLString) {
continue
}
if (numOpeningBrackets == 0) {
openBracketPos = index
}
numOpeningBrackets += 1
case "]":
if (foundURLString) {
resetVariables()
break
}
numOpeningBrackets -= 1
if (numOpeningBrackets <= 0) {
closeBracketPos = index
foundURLString = true
}
case "(":
if (!foundURLString) {
continue
}
if openParenPos != nil {
resetVariables()
break
}
openParenPos = index
case ")":
if (!foundURLString) {
continue
}
if (openParenPos == nil || closeParenPos != nil) {
resetVariables()
break
}
closeParenPos = index
// Create ranges of form Range<String.Index>
let totalRange = text.characters.index(text.startIndex, offsetBy: openBracketPos!)..<text.characters.index(text.startIndex, offsetBy: closeParenPos! + 1)
let linkRange = text.characters.index(text.startIndex, offsetBy: openBracketPos! + 1)..<text.characters.index(text.startIndex, offsetBy: closeBracketPos!)
let addressRange = text.characters.index(text.startIndex, offsetBy: openParenPos! + 1)..<text.characters.index(text.startIndex, offsetBy: closeParenPos!)
let markdownDict = [KEY_RANGE_TOTAL: totalRange, KEY_RANGE_LINK: linkRange, KEY_RANGE_ADDRESS: addressRange]
markdownArray.append(markdownDict)
resetVariables()
case " ":
break;
default:
if (openParenPos == nil && foundURLString) {
resetVariables()
}
}
}
if (markdownArray.count > 0) {
return markdownArray
}
return nil
}
open func textView(_ textView: UITextView, shouldInteractWithURL URL: Foundation.URL, inRange characterRange: NSRange) -> Bool {
return true
}
}