본문 바로가기
#모바일 [Mobile]/iOS

[iOS] NSMutableAttributedString를 통한 UILabel에 Image 삽입하기 (How to insert images into an attributed string with NSTextAttachment)

by cy_mos 2019. 7. 3.
반응형

https://stackoverflow.com/questions/32772007/how-to-add-images-as-text-attachment-in-swift-using-nsattributedstring


📄 Swift Source Code

NSMutableAttributedString+Append.swift
0.00MB

import UIKit

// MARK: - Extension NSAttributedString Image Protocol
extension NSMutableAttributedString {
    func appendImage(_ image: UIImage) {
        // MARK: Create our NSTextAttachment.
        let imageAttchment: NSTextAttchment = NSTextAttchment()
        imageAttchment.image = image

        // MARK: Wrap the attachment in its own attributed string so we can append it.
        let strImage: NSAttributedString = NSAttributedString(attachment: imageAttchment)
        self.append(strImage)
    }
}

🚀 REFERENCE

 

How to insert images into an attributed string with NSTextAttachment - free Swift 5.0 example code and tips

Was this page useful? Let me know! 1 2 3 4 5

www.hackingwithswift.com

 

How to add images as text attachment in Swift using nsattributedstring

I'm trying to build a custom keyboard for iOS using images which I've put in as buttons. When I press a button, the image linked to the button is put into an attributed string which is loaded into an

stackoverflow.com

 

반응형

댓글