2014-11-04 9 views
5

Ich folgte der following post wie NSTextAttachment verwenden, um Bilder inline mit Ihren UILabels hinzuzufügen. Ich folgte dem Besten, was ich konnte und schrieb meine Version in Swift.Wie füge ich ein Bild Inline UILabel in iOS 8 mit swift

Ich erstelle eine Chat-Anwendung und das Feld, in das ich ein Bier-Symbol einfüge, rendert das Bild nicht oder scheint das Bild nicht inline darzustellen. Ich bekomme keine Fehler, deshalb nehme ich an, dass mir ein kleines Detail in meinem Code fehlt.

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; 
+0

Wie groß ist die Bildgröße? – Larme

Antwort

16

Dies funktioniert nicht auf einem UITextField. Es funktioniert nur auf einem UILabel.

ist hier ein UILabel Erweiterung basiert auf dem Code (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 
    } 
} 

EDIT:

hier ist eine neue Version, die das Symbol vor oder nach der Beschriftung hinzufügen können. Es gibt auch eine Funktion auf das Symbol auf dem Etikett

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 
    } 
} 
+2

danke paaren Sie rockt –

+0

Wenn Sie auf Hinzufügen mehrerer Bilder planen, Erinnern Sie sich die Initializer von lassen strLabelText zu ändern: NSAttributedString = NSAttributedString (string: self.text) zu: lassen strLabelText: NSMutableAttributedString = NSMutableAttributedString (attributedString: selbstgeladenerText!) –

5

Regis St-Gelais der Erweiterung Antwort für Swift 3 und Swift 4 und ohne gezwungen Abwickeln zu entfernen:

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 
    } 
} 

Verbrauch:

self.theLabel.text = "desiredText" 
self.theLabel.addImageWith(name: "nameOfImage", behindText: false) 
Verwandte Themen