diff --git a/CHANGELOG.md b/CHANGELOG.md index 7539df157..e04bd0d12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa - Added `shouldManageSendButtonEnabledState` to `MessageInputBar` to disable automatically managing `MessageInputBar.sendButton`'s `isEnabled` state when text changes. (Default value is `true`). [#530](https://github.com/MessageKit/MessageKit/pull/530) by [@clayellis](https://github.com/clayellis). +### Fixed + +- Fix wrong separated components in messageInputBar. +[#577](https://github.com/MessageKit/MessageKit/pull/577) by [@zhongwuzw](https://github.com/zhongwuzw). + ## [[Prerelease] 0.13.1](https://github.com/MessageKit/MessageKit/releases/tag/0.13.1) ### Fixed diff --git a/Sources/Views/InputTextView.swift b/Sources/Views/InputTextView.swift index d9c53776b..3a8864329 100644 --- a/Sources/Views/InputTextView.swift +++ b/Sources/Views/InputTextView.swift @@ -303,26 +303,50 @@ open class InputTextView: UITextView { private func parseForComponents() -> [Any] { var components = [Any]() - let range = NSRange(location: 0, length: attributedText.length) - attributedText.enumerateAttributes(in: range, options: []) { (object, range, _) in - - if object.keys.contains(.attachment) { - if let attachment = object[.attachment] as? NSTextAttachment { - if let image = attachment.image { - components.append(image) - } else if let image = attachment.image(forBounds: attachment.bounds, - textContainer: nil, - characterIndex: range.location) { - components.append(image) + var attachments = [(NSRange, UIImage)]() + let length = attributedText.length + let range = NSRange(location: 0, length: length) + attributedText.enumerateAttribute(.attachment, in: range) { (object, range, _) in + if let attachment = object as? NSTextAttachment { + if let image = attachment.image { + attachments.append((range, image)) + } else if let image = attachment.image(forBounds: attachment.bounds, + textContainer: nil, + characterIndex: range.location) { + attachments.append((range,image)) + } + } + } + + var curLocation = 0 + if attachments.count == 0 { + let text = attributedText.string.trimmingCharacters(in: .whitespacesAndNewlines) + if !text.isEmpty { + components.append(text) + } + } + else { + attachments.forEach { (attachment) in + let (range, image) = attachment + if curLocation < range.location { + let textRange = NSMakeRange(curLocation, range.location) + let text = attributedText.attributedSubstring(from: textRange).string.trimmingCharacters(in: .whitespacesAndNewlines) + if !text.isEmpty { + components.append(text) } } - } else { - let stringValue = attributedText.attributedSubstring(from: range).string.trimmingCharacters(in: .whitespacesAndNewlines) - if !stringValue.isEmpty { - components.append(stringValue) + + curLocation = range.location + range.length + components.append(image) + } + if curLocation < length - 1 { + let text = attributedText.attributedSubstring(from: NSMakeRange(curLocation, length - curLocation)).string.trimmingCharacters(in: .whitespacesAndNewlines) + if !text.isEmpty { + components.append(text) } } } + return components }