Skip to content

Commit 4fb1662

Browse files
authored
Fix lots of bugs (#59)
1 parent b3d2c67 commit 4fb1662

File tree

6 files changed

+81
-23
lines changed

6 files changed

+81
-23
lines changed

MessageViewController.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
290482271FED90340053978C /* UITextView+Prefixes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2904821E1FED90340053978C /* UITextView+Prefixes.swift */; };
1818
29792B141FFAE7FC007A0C57 /* MessageTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29792B121FFAE7FC007A0C57 /* MessageTextView.swift */; };
1919
29792B151FFAE7FC007A0C57 /* MessageAutocompleteController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29792B131FFAE7FC007A0C57 /* MessageAutocompleteController.swift */; };
20+
29C8F9A9208BDAC60075931C /* ExpandedHitTestButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C8F9A8208BDAC60075931C /* ExpandedHitTestButton.swift */; };
2021
29CC293B1FF4266D006B6DE7 /* MessageViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29CC293A1FF4266D006B6DE7 /* MessageViewControllerTests.swift */; };
2122
29CC293D1FF4266D006B6DE7 /* MessageViewController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2904820B1FED90070053978C /* MessageViewController.framework */; };
2223
29CC29441FF4267F006B6DE7 /* String+WordAtRangeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29CC29431FF4267F006B6DE7 /* String+WordAtRangeTests.swift */; };
@@ -50,6 +51,7 @@
5051
2904821E1FED90340053978C /* UITextView+Prefixes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UITextView+Prefixes.swift"; sourceTree = "<group>"; };
5152
29792B121FFAE7FC007A0C57 /* MessageTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageTextView.swift; sourceTree = "<group>"; };
5253
29792B131FFAE7FC007A0C57 /* MessageAutocompleteController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageAutocompleteController.swift; sourceTree = "<group>"; };
54+
29C8F9A8208BDAC60075931C /* ExpandedHitTestButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpandedHitTestButton.swift; sourceTree = "<group>"; };
5355
29CC29381FF4266D006B6DE7 /* MessageViewControllerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MessageViewControllerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5456
29CC293A1FF4266D006B6DE7 /* MessageViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageViewControllerTests.swift; sourceTree = "<group>"; };
5557
29CC293C1FF4266D006B6DE7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -103,6 +105,7 @@
103105
2904820F1FED90070053978C /* Info.plist */,
104106
29792B131FFAE7FC007A0C57 /* MessageAutocompleteController.swift */,
105107
29792B121FFAE7FC007A0C57 /* MessageTextView.swift */,
108+
29C8F9A8208BDAC60075931C /* ExpandedHitTestButton.swift */,
106109
290482181FED90340053978C /* MessageView.swift */,
107110
2904820E1FED90070053978C /* MessageViewController.h */,
108111
290482171FED90340053978C /* MessageViewController.swift */,
@@ -252,6 +255,7 @@
252255
2904821F1FED90340053978C /* UIScrollView+StopScrolling.swift in Sources */,
253256
29CC29471FF42687006B6DE7 /* String+WordAtRange.swift in Sources */,
254257
290482271FED90340053978C /* UITextView+Prefixes.swift in Sources */,
258+
29C8F9A9208BDAC60075931C /* ExpandedHitTestButton.swift in Sources */,
255259
);
256260
runOnlyForDeploymentPostprocessing = 0;
257261
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// ExpandedHitTestButton.swift
3+
// MessageViewController
4+
//
5+
// Created by Ryan Nystrom on 4/21/18.
6+
// Copyright © 2018 Ryan Nystrom. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
internal final class ExpandedHitTestButton: UIButton {
12+
13+
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
14+
guard isHidden == false, window != nil else { return false }
15+
return bounds.insetBy(dx: -10, dy: -10).contains(point)
16+
}
17+
18+
}

MessageViewController/MessageAutocompleteController.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ public final class MessageAutocompleteController: MessageTextViewListener {
3333
/// Adds an additional space after the autocompleted text when true. Default value is `TRUE`
3434
open var appendSpaceOnCompletion = true
3535

36-
/// The default text attributes
37-
open var defaultTextAttributes: [NSAttributedStringKey: Any] = [.font: UIFont.preferredFont(forTextStyle: .body), .foregroundColor: UIColor.black]
38-
3936
/// The text attributes applied to highlighted substrings for each prefix
4037
private var autocompleteTextAttributes: [String: [NSAttributedStringKey: Any]] = [:]
4138

4239
/// A key used for referencing which substrings were autocompletes
4340
private let NSAttributedAutocompleteKey = NSAttributedStringKey.init("com.messageviewcontroller.autocompletekey")
41+
42+
private var defaultTextAttributes: [NSAttributedStringKey: Any] {
43+
return [
44+
.font: textView.defaultFont,
45+
.foregroundColor: textView.defaultTextColor,
46+
]
47+
}
4448

4549
/// A reference to `defaultTextAttributes` that adds the NSAttributedAutocompleteKey
4650
private var typingTextAttributes: [NSAttributedStringKey: Any] {
4751
var attributes = defaultTextAttributes
48-
attributes[NSAttributedAutocompleteKey] = false
4952
attributes[.paragraphStyle] = paragraphStyle
53+
attributes[NSAttributedAutocompleteKey] = false
5054
return attributes
5155
}
5256

@@ -178,10 +182,17 @@ public final class MessageAutocompleteController: MessageTextViewListener {
178182
// MARK: Private API
179183

180184
private func insertAutocomplete(_ autocomplete: String, at selection: Selection, for range: NSRange, keepPrefix: Bool) {
181-
182-
// Apply the autocomplete attributes
183-
var attrs = autocompleteTextAttributes[selection.prefix] ?? defaultTextAttributes
185+
let defaultTypingTextAttributes = typingTextAttributes
186+
187+
var attrs = defaultTextAttributes
184188
attrs[NSAttributedAutocompleteKey] = true
189+
190+
if let autoAttrs = autocompleteTextAttributes[selection.prefix] {
191+
for (k, v) in autoAttrs {
192+
attrs[k] = v
193+
}
194+
}
195+
185196
let newString = (keepPrefix ? selection.prefix : "") + autocomplete
186197
let newAttributedString = NSAttributedString(string: newString, attributes: attrs)
187198

@@ -192,7 +203,7 @@ public final class MessageAutocompleteController: MessageTextViewListener {
192203
// Replace the attributedText with a modified version including the autocompete
193204
let newAttributedText = textView.attributedText.replacingCharacters(in: highlightedRange, with: newAttributedString)
194205
if appendSpaceOnCompletion {
195-
newAttributedText.append(NSAttributedString(string: " ", attributes: typingTextAttributes))
206+
newAttributedText.append(NSAttributedString(string: " ", attributes: defaultTypingTextAttributes))
196207
}
197208
textView.attributedText = newAttributedText
198209
}

MessageViewController/MessageTextView.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,24 @@ open class MessageTextView: UITextView, UITextViewDelegate {
2424
set {}
2525
}
2626

27+
open var defaultFont: UIFont? = UIFont.preferredFont(forTextStyle: .body)
28+
29+
open var defaultTextColor: UIColor? = .black
30+
2731
open override var font: UIFont? {
2832
didSet {
33+
defaultFont = font
2934
placeholderLabel.font = font
3035
placeholderLayoutDidChange()
3136
}
3237
}
3338

39+
open override var textColor: UIColor? {
40+
didSet {
41+
defaultTextColor = textColor
42+
}
43+
}
44+
3445
open override var textAlignment: NSTextAlignment {
3546
didSet {
3647
placeholderLabel.textAlignment = textAlignment

MessageViewController/MessageView.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ public final class MessageView: UIView, MessageTextViewListener {
1313
public let textView = MessageTextView()
1414

1515
internal weak var delegate: MessageViewDelegate?
16-
internal let leftButton = UIButton()
17-
internal let rightButton = UIButton()
16+
internal let leftButton = ExpandedHitTestButton()
17+
internal let rightButton = ExpandedHitTestButton()
1818
internal let UITextViewContentSizeKeyPath = #keyPath(UITextView.contentSize)
1919
internal let topBorderLayer = CALayer()
2020
internal var contentView: UIView?
21-
internal var leftButtonAction: Selector?
2221
internal var rightButtonAction: Selector?
2322
internal var leftButtonInset: CGFloat = 0
2423
internal var rightButtonInset: CGFloat = 0
@@ -111,7 +110,7 @@ public final class MessageView: UIView, MessageTextViewListener {
111110
}
112111
}
113112

114-
public var inset: UIEdgeInsets {
113+
public var textViewInset: UIEdgeInsets {
115114
set {
116115
textView.textContainerInset = newValue
117116
setNeedsLayout()
@@ -206,7 +205,6 @@ public final class MessageView: UIView, MessageTextViewListener {
206205
switch position {
207206
case .left:
208207
button = leftButton
209-
leftButtonAction = action
210208
case .right:
211209
button = rightButton
212210
rightButtonAction = action
@@ -242,6 +240,12 @@ public final class MessageView: UIView, MessageTextViewListener {
242240
buttonLayoutDidChange(button: button)
243241
}
244242

243+
public var bottomInset: CGFloat = 0 {
244+
didSet {
245+
delegate?.wantsLayout(messageView: self)
246+
}
247+
}
248+
245249
// MARK: Overrides
246250

247251
public override func layoutSubviews() {
@@ -270,36 +274,36 @@ public final class MessageView: UIView, MessageTextViewListener {
270274

271275
// adjust by bottom offset so content is flush w/ text view
272276
let leftButtonFrame = CGRect(
273-
x: safeBounds.minX + inset.left,
274-
y: textViewMaxY - leftButtonSize.height + leftButton.bottomHeightOffset - inset.bottom,
277+
x: safeBounds.minX + leftButtonInset,
278+
y: textViewMaxY - leftButtonSize.height + leftButton.bottomHeightOffset - textViewInset.bottom,
275279
width: leftButtonSize.width,
276280
height: leftButtonSize.height
277281
)
278282
leftButton.frame = showLeftButton ? leftButtonFrame : .zero
279283

284+
let leftButtonMaxX = (showLeftButton ? leftButtonFrame.maxX : 0)
280285
let textViewFrame = CGRect(
281-
x: leftButtonFrame.maxX + leftButtonInset,
286+
x: (showLeftButton ? leftButtonMaxX + leftButtonInset : 0),
282287
y: textViewY,
283-
width: safeBounds.width - leftButtonFrame.maxX - rightButtonSize.width - rightButtonInset - inset.right,
288+
width: safeBounds.width - leftButtonMaxX - rightButtonSize.width - rightButtonInset,
284289
height: textViewHeight
285290
)
286291
textView.frame = textViewFrame
287292

288293
// adjust by bottom offset so content is flush w/ text view
289294
let rightButtonFrame = CGRect(
290-
x: textViewFrame.maxX + rightButtonInset,
291-
y: textViewMaxY - rightButtonSize.height + rightButton.bottomHeightOffset - inset.bottom,
295+
x: textViewFrame.maxX,
296+
y: textViewMaxY - rightButtonSize.height + rightButton.bottomHeightOffset - textViewInset.bottom,
292297
width: rightButtonSize.width,
293298
height: rightButtonSize.height
294299
)
295300
rightButton.frame = rightButtonFrame
296301

297-
let contentY = textViewFrame.maxY + inset.bottom
298302
contentView?.frame = CGRect(
299303
x: safeBounds.minX,
300-
y: contentY,
304+
y: textViewFrame.maxY,
301305
width: safeBounds.width,
302-
height: bounds.height - contentY - util_safeAreaInsets.bottom
306+
height: contentView?.frame.height ?? 0
303307
)
304308
}
305309

@@ -316,7 +320,9 @@ public final class MessageView: UIView, MessageTextViewListener {
316320
// MARK: Private API
317321

318322
internal var height: CGFloat {
319-
return textViewHeight + (contentView?.bounds.height ?? 0)
323+
return textViewHeight
324+
+ (contentView?.bounds.height ?? 0)
325+
+ bottomInset
320326
}
321327

322328
internal var maxLineHeight: CGFloat {

0 commit comments

Comments
 (0)