Atributika icon indicating copy to clipboard operation
Atributika copied to clipboard

add display img function

Open airy-f opened this issue 3 years ago • 1 comments

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)

airy-f avatar Nov 08 '21 13:11 airy-f

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
}

wujingcheng7 avatar Aug 29 '23 09:08 wujingcheng7