Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 39 additions & 15 deletions Sources/Views/InputTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhongwuzw Can you help me understand the code from here? I just want to understand before we merge if Nathan doesn't have time to look at this one

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SD10 , First we should enumerate all .attachment, get its image and range, then we can split text and image based on the range.

if attachments.count == 0 {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SD10 If no images, just text.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add text and image to components

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 {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After last image, maybe still has text, so we should append it to components.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is length - 1 here? Sorry 😓

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 It's because we add a new line after image, so we decrease 1 to prevent unnecessary calculation.

let text = attributedText.attributedSubstring(from: NSMakeRange(curLocation, length - curLocation)).string.trimmingCharacters(in: .whitespacesAndNewlines)
if !text.isEmpty {
components.append(text)
}
}
}

return components
}

Expand Down