Skip to content

Support to insert images on text using NSTextAttachament #6

Description

@unnamedd

I believe that support NSTextAttachment image could be a good feature to have on lib

Activity

  1. unnamedd commented on Mar 6, 2017

    @unnamedd
    Author

    Sorry. I found on Style.swift support to NSTextAttachment, but not on README.

  2. av0c0der commented on Sep 26, 2017

    @av0c0der

    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?

  3. unnamedd commented on Sep 26, 2017

    @unnamedd
    Author

    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-nstextattachment

    here 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
    })
  4. mukyasa commented on Dec 18, 2017

    @mukyasa

    Hello ,

    Did anyone find the solution to add NSTextAttachment i am facing difficulty adding it. The above solution didn't work for me

  5. GabrielCartier commented on Apr 22, 2019

    @GabrielCartier

    Hello ,

    Did anyone find the solution to add NSTextAttachment i am facing difficulty adding it. The above solution didn't work for me

    A 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

  6. added a commit that references this issue on Dec 21, 2019
  7. self-assigned this
    on Dec 21, 2019
  8. added this to the 3.5.0 milestone on Dec 21, 2019
  9. added a commit that references this issue on Dec 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions