Atributika
Atributika copied to clipboard
add display img function
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)
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
}