// attachment
        let textAttachment: NSTextAttachment = NSTextAttachment()
        textAttachment.image = UIImage(named: "icon.png")
        
        // center font
        if let image = textAttachment.image,
            let _font = font {
            let y = ((_font.ascender - _font.descender)/2 - image.size.height/2)
            textAttachment.bounds = CGRect(x: 0, y: y, width: image.size.width, height: image.size.height).integral
        }
        let attributedImage =  NSAttributedString(attachment: textAttachment)

xem thêm : học lập trình android ở đâu