如何使用swift在iOS 8中插入图像内联UILabel

mat*_*ace 11 nstextattachment swift ios8

我按照以下文章介绍了如何使用NSTextAttachment添加与UILabel内联的图像.我尽我所能,并在Swift中编写了我的版本.

我正在创建一个聊天应用程序,我正在插入啤酒图标的字段不会渲染图像或似乎没有内嵌渲染图像.我没有收到任何错误,所以我假设我在代码中遗漏了一些小细节.

var beerName:String!

        if(sender == bn_beer1)
        {
            beerName = "beer1.png"
        }

        if(sender == bn_beer2)
        {
            beerName = "beer2.png"
        }

        if(sender == bn_beer3)
        {
            beerName = "beer3"
        }



        var attachment:NSTextAttachment = NSTextAttachment()
        attachment.image = UIImage(named: beerName)


        var attachmentString:NSAttributedString = NSAttributedString(attachment: attachment)
        var myString:NSMutableAttributedString = NSMutableAttributedString(string: inputField.text)
        myString.appendAttributedString(attachmentString)



        inputField.attributedText = myString;
Run Code Online (Sandbox Code Playgroud)

Reg*_*ais 30

这不适用于UITextField.它只适用于UILabel.

这是基于您的代码的UILabel扩展(Swift 2.0)

extension UILabel
{
    func addImage(imageName: String)
    {
        let attachment:NSTextAttachment = NSTextAttachment()
        attachment.image = UIImage(named: imageName)

        let attachmentString:NSAttributedString = NSAttributedString(attachment: attachment)
        let myString:NSMutableAttributedString = NSMutableAttributedString(string: self.text!)
        myString.appendAttributedString(attachmentString)

        self.attributedText = myString
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

这是一个新版本,允许在标签之前或之后添加图标.还有一个从标签中删除图标的功能

extension UILabel
{
    func addImage(imageName: String, afterLabel bolAfterLabel: Bool = false)
    {
        let attachment: NSTextAttachment = NSTextAttachment()
        attachment.image = UIImage(named: imageName)
        let attachmentString: NSAttributedString = NSAttributedString(attachment: attachment)

        if (bolAfterLabel)
        {
            let strLabelText: NSMutableAttributedString = NSMutableAttributedString(string: self.text!)
            strLabelText.appendAttributedString(attachmentString)

            self.attributedText = strLabelText
        }
        else
        {
            let strLabelText: NSAttributedString = NSAttributedString(string: self.text!)
            let mutableAttachmentString: NSMutableAttributedString = NSMutableAttributedString(attributedString: attachmentString)
            mutableAttachmentString.appendAttributedString(strLabelText)

            self.attributedText = mutableAttachmentString
        }
    }

    func removeImage()
    {
        let text = self.text
        self.attributedText = nil
        self.text = text
    }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*eek 12

Regis St-Gelais对Swift 3Swift 4的扩展答案,没有强制解包:

extension UILabel {

    func addImageWith(name: String, behindText: Bool) {

        let attachment = NSTextAttachment()
        attachment.image = UIImage(named: name)
        let attachmentString = NSAttributedString(attachment: attachment)

        guard let txt = self.text else {
            return
        }

        if behindText {
            let strLabelText = NSMutableAttributedString(string: txt)
            strLabelText.append(attachmentString)
            self.attributedText = strLabelText
        } else {
            let strLabelText = NSAttributedString(string: txt)
            let mutableAttachmentString = NSMutableAttributedString(attributedString: attachmentString)
            mutableAttachmentString.append(strLabelText)
            self.attributedText = mutableAttachmentString
        }
    }

    func removeImage() {
        let text = self.text
        self.attributedText = nil
        self.text = text
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

self.theLabel.text = "desiredText"
self.theLabel.addImageWith(name: "nameOfImage", behindText: false)
Run Code Online (Sandbox Code Playgroud)


Jor*_*ray 6

编辑19/03/18:更正imageBehindText = false时的错误+像素中的图像大小没有任何意义.

大卫的功能更新为多个图像保存文本和图像大小基于字体大小(Swift 4):

extension UILabel {
    /**
     This function adding image with text on label.

     - parameter text: The text to add
     - parameter image: The image to add
     - parameter imageBehindText: A boolean value that indicate if the imaga is behind text or not
     - parameter keepPreviousText: A boolean value that indicate if the function keep the actual text or not
     */
    func addTextWithImage(text: String, image: UIImage, imageBehindText: Bool, keepPreviousText: Bool) {
                    let lAttachment = NSTextAttachment()
        lAttachment.image = image

        // 1pt = 1.32px
        let lFontSize = round(self.font.pointSize * 1.32)
        let lRatio = image.size.width / image.size.height

        lAttachment.bounds = CGRect(x: 0, y: ((self.font.capHeight - lFontSize) / 2).rounded(), width: lRatio * lFontSize, height: lFontSize)

        let lAttachmentString = NSAttributedString(attachment: lAttachment)

        if imageBehindText {
            let lStrLabelText: NSMutableAttributedString

            if keepPreviousText, let lCurrentAttributedString = self.attributedText {
                lStrLabelText = NSMutableAttributedString(attributedString: lCurrentAttributedString)
                lStrLabelText.append(NSMutableAttributedString(string: text))
            } else {
                lStrLabelText = NSMutableAttributedString(string: text)
            }

            lStrLabelText.append(lAttachmentString)
            self.attributedText = lStrLabelText
        } else {
            let lStrLabelText: NSMutableAttributedString

            if keepPreviousText, let lCurrentAttributedString = self.attributedText {
                lStrLabelText = NSMutableAttributedString(attributedString: lCurrentAttributedString)
                lStrLabelText.append(NSMutableAttributedString(attributedString: lAttachmentString))
                lStrLabelText.append(NSMutableAttributedString(string: text))
            } else {
                lStrLabelText = NSMutableAttributedString(attributedString: lAttachmentString)
                lStrLabelText.append(NSMutableAttributedString(string: text))
            }

            self.attributedText = lStrLabelText
        }
    }

    func removeImage() {
        let text = self.text
        self.attributedText = nil
        self.text = text
    }
}
Run Code Online (Sandbox Code Playgroud)