Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add custom images #17

Open
Topwiz opened this issue Apr 6, 2021 · 2 comments
Open

How do I add custom images #17

Topwiz opened this issue Apr 6, 2021 · 2 comments
Labels
question Further information is requested

Comments

@Topwiz
Copy link

Topwiz commented Apr 6, 2021

I was adding image attachments with NSTextAttachment in my custom textView.(Like below)
let textAttachment = NSTextAttachment() textAttachment.bounds = CGRect(x: 0, y: (textView.font!.capHeight - image.size.height).rounded() / 2, width: image.size.width, height: image.size.height) textAttachment.image = image let attrStringWithImage = NSAttributedString(attachment: textAttachment) textView.textStorage.replaceCharacters(in: currentRange, with: attrStringWithImage)
Is there a way to add images(don't care if is it an NSTextAttachment or a suffixedAttachment) on the textView with TwitterTextEditor?

I found I can get hashtags with regex and place an UIImage or UIView after that hashtag. But I can't find a way how to add just UIImage itself like an NSTextAttachment.

@niw
Copy link
Contributor

niw commented Apr 17, 2021

Hi, @Topwiz.

This is actually bit difficult on current Twitter Text Editor API design, since it is based on plain text not attributed text.

To use NSTextAttachment to render image within the text view (which is possible in Twitter Text Editor,) but it need to add that attribute to the U+FFFC (NSTextAttachment.character) in the plain text, however Twitter Text Editor API is, intentionally separate plain text updating and attribute updating logic, therefore it is bit difficult to use NSTextAttachment.

I understand however such use case may exist, so will explore new API for that.

@MainasuK
Copy link
Contributor

@Topwiz

I use the TwitterTextEditor layoutManager as the layout backend. And set the image placeholder with space and keep the range. Then replace that space with SuffixedAttachment. It works. Maybe that is what you need.

https://github.com/TwidereProject/ActiveLabel.swift/blob/master/Sources/ActiveLabel/ActiveLabel.swift#L312-L342

@niw niw added the question Further information is requested label Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants