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

add display img function #147

Open
airy-f opened this issue Nov 8, 2021 · 1 comment
Open

add display img function #147

airy-f opened this issue Nov 8, 2021 · 1 comment

Comments

@airy-f
Copy link

airy-f commented Nov 8, 2021

Is there a way to display img tags?
I know it's a lot of work, but I need you to add the ability to show img tags!

        let attachment = NSTextAttachment()
        attachment.image = image

        let y = (font.capHeight-size.height).rounded() / 2
        attachment.bounds.origin = CGPoint(x: 0, y: y)
        attachment.bounds.size = size

        let imageAttribute = NSAttributedString(attachment: attachment)
        let mutableString = NSMutableAttributedString(string: text)
        mutableString.insert(imageAttribute, at: text.count)
@wujingcheng7
Copy link

wujingcheng7 commented Aug 29, 2023

from Demo code

func stringWithImage() -> NSAttributedString {
    let font = UIFont(name: "HelveticaNeue-BoldItalic", size: 12)!

    let b = Attrs().font(font).underlineStyle(.single)

    let img = TagTuner(style: {
        let style = Attrs()
        if let imageId = $0.tag.attributes["id"] {
            let textAttachment = NSTextAttachment()
            textAttachment.image = UIImage(named: imageId)
            style.attachment(textAttachment)
        }
        return style
    }, transform: { _, part in
        switch part {
        case .opening:
            return "\u{FFFC}"
        case .closing:
            return nil
        case .content:
            return nil
        }
    })

    let str = "<b>Running</b> with <img id=\"scissors\"></img><img id=\"scissors\"/></img id=\"scissors\">!"
        .style(tags: ["b": b, "img": img])
        .attributedString

    return str
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants