Repository navigation
Support to insert images on text using NSTextAttachament #6
Description
Activity
Sorry. I found on Style.swift support to NSTextAttachment, but not on README.
Didn't want to open new issue, since is will be the duplicate of this one.
I'm unable to get it to work. I create a Style, set an attach with an image, apply to the text and nothing changes there. Used default implementation for now, but wanted to get rid off that.
@unnamedd did you get able to set an attachment?
Hi, @jauzee
here you can find a post explaining how to use NSTextAttachment to AttributedString:
https://www.hackingwithswift.com/example-code/system/how-to-insert-images-into-an-attributed-string-with-nstextattachmenthere is where SwiftRichString works NSTextAttachments:
/// The value of this attribute is an NSTextAttachment object. The default value of this property is nil, indicating no attachment. Here is some example that could work for you. I didn't test this piece of code, so it's by yourself, if you have another question, just comment here again.
// create our NSTextAttachment let image1Attachment = NSTextAttachment() image1Attachment.image = UIImage(named: "yourIcon.png") // create our valueStyle let valueStyle = Style("value", { $0.font = FontAttribute(.HelveticaNeue_Bold, size: 18) $0.color = UIColor(red: 85/255, green: 85/255, blue: 85/255, alpha: 1) $0.align = .center $0.attach = image1Attachment })
Hello ,
Did anyone find the solution to add
NSTextAttachmenti am facing difficulty adding it. The above solution didn't work for meHello ,
Did anyone find the solution to add
NSTextAttachmenti am facing difficulty adding it. The above solution didn't work for meA bit late, but the key is to add your attachment to your
NSAttributedString. The code given does not work indeed. Something like this would work:let imageAttachment = NSTextAttachment() imageAttachment.image = UIImage(named: "your_image") let attributedString = NSMutableAttributedString(attachment: imageAttachment) attributedString.append(NSAttributedString(string: "your text"))
Then you can simply apply the style you want on the
AttributedString- added a commit that references this issue
on Dec 21, 2019 - added a commit that references this issue
on Dec 21, 2019
I believe that support NSTextAttachment image could be a good feature to have on lib