fphilipe / PHFComposeBarView

Compose bar from iOS 7 Messages.app
MIT License
1.18k stars 204 forks source link

Unable to scroll to caret after inserting NSTextAttachment in textView #52

Closed bhushanb02 closed 8 years ago

bhushanb02 commented 8 years ago

I am adding images to compose bar’s textview using NSTextAttachment. But when I add more than 2-3 images, textview is not exactly scrolling to caret. Also when I have a combination of text and images (image at last) and when I tap on last line of compose bar, it does not scroll to last line, it scrolls on first or second line or image. Please check my code :

let attachment = ChatTextAttachment()
attachment.image = thumbImage

// Add NSTextAttachment into attributedString
let attributedString = NSMutableAttributedString(string: "", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(14)])
let attrStringWithImage = NSAttributedString(attachment: attachment)
attributedString.appendAttributedString(attrStringWithImage)
attributedString.appendAttributedString(NSAttributedString(string: "\n", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(14)]))

// Add this attributed string to the current position.
self.composeBar.textView.textStorage.insertAttributedString(attributedString, atIndex: self.composeBar.textView.selectedRange.location)
self.composeBar.textView.select(self)
self.composeBar.textView.selectedRange = NSMakeRange(self.composeBar.textView.attributedText.length, 0 );

self.textViewDidChange(self.composeBar.textView)
self.composeBar.textView.layoutIfNeeded()

ChatTextAttachment is a subclass of NSTextAttachment Let me know if you need any more details.

fphilipe commented 8 years ago

I'm sorry, can't help you. I recommend asking on StackOverflow.